I don't understand why I get the correct count of my array when using ForEach, but not when I pipe to ForEach-Object. The piped count is always 0.
Correct Count:
$hash = @(Get-ChildItem C:\Dir -Recurse -Include *.txt | Where {$_.length -gt 0})
ForEach ($i in @hash) {
Write-Host = $i.BaseName
}
"Array Count = $($hash.Count)"
Output:
File1.txt
File2.txt
Array Count = 2
Incorrect Count:
$hash = @(Get-ChildItem C:\Dir -Recurse -Include *.txt | Where {$_.length -gt 0}) | ForEach-Object {
Write-Host $_.BaseName
}
"Array Count = $($hash.Count)"
Output:
File1.txt
File2.txt
Array Count = 0