Skip to main content

PowerShell example of looping over files stored in an array.

#
# To iterate over files matched in an array
# http://stackoverflow.com/a/29440787
$files = @(Get-ChildItem c:\path\to\files\*.txt)
foreach ($file in $files)
{
    Write-Host $file
}

# Alternatively (http://stackoverflow.com/a/18848848):
Get-ChildItem $SourceFolder -Filter $MatchFilePattern -File |
    ForEach-Object
    {
        Write-Host $_.Directory
        Write-Host $_.Name
    }