Zulfiqar Alibhai
Zulfiqar Alibhai

Reputation: 61

How do I return a filename from a forEach-Object loop in Powershell?

I have been using Powershell for a day now and I need to return the file name for each file in a folder using a loop. Here is what I currently have:

$filePath = 'C:\Users\alibh\Desktop\Test Folder' #the path to the folder
cd $filePath

Get-ChildItem $filePath |
ForEach-Object{
$fileName = "here is where I return the name of each file so I can edit it 
later on"
}

I want to compare the names of the different files in the folder and edit or delete files later on; but before I can get to that, I first need to be able to get the name of each file one after another.

Edit: thanks a lot guys

Upvotes: 6

Views: 19163

Answers (2)

AdminOfThings
AdminOfThings

Reputation: 25061

For each filename only within your loop, you can do the following:

Get-ChildItem $filepath -File | Foreach-Object {
    $fileName = $_.Name
    $fileName   # Optional for returning the file name to the console
}

For each filename and its path only within your loop, you can do the following:

Get-ChildItem $filepath -File | Foreach-Object {
    $fileName = $_.FullName
}

Explanation:

With this code structure, you will only have access to each of those filenames within the Foreach-Object script block by default with the exception of the last object passed into the loop. $_ or $PSItem represents the current object within the Foreach-Object {} script block. It will contain all of the properties of a single object returned by Get-ChildItem. You can effectively see all of the properties accessible to the $_ variable by either piping the Get-ChildItem results into Get-Member or the $_ variable itself like so:

Get-ChildItem $filepath -File | Get-Member -MemberType Property

   TypeName: System.IO.FileInfo

Name              MemberType Definition
----              ---------- ----------
Attributes        Property   System.IO.FileAttributes Attributes {get;set;}
CreationTime      Property   datetime CreationTime {get;set;}
CreationTimeUtc   Property   datetime CreationTimeUtc {get;set;}
Directory         Property   System.IO.DirectoryInfo Directory {get;}
DirectoryName     Property   string DirectoryName {get;}
Exists            Property   bool Exists {get;}
Extension         Property   string Extension {get;}
FullName          Property   string FullName {get;}
IsReadOnly        Property   bool IsReadOnly {get;set;}
LastAccessTime    Property   datetime LastAccessTime {get;set;}
LastAccessTimeUtc Property   datetime LastAccessTimeUtc {get;set;}
LastWriteTime     Property   datetime LastWriteTime {get;set;}
LastWriteTimeUtc  Property   datetime LastWriteTimeUtc {get;set;}
Length            Property   long Length {get;}
Name              Property   string Name {get;}

Upvotes: 7

js2010
js2010

Reputation: 27616

Here's an odd workaround to get the full path of each file (in a string context), add a wildcard to the folder path:

Get-ChildItem $filePath\* | ForEach { "$_" }

Upvotes: 2

Related Questions