0

$ll = @("a","b","c"); ForEach ($e in $ll) {Write-Host $_}

I've spent too much time on this already and I'm tired of searching and fiddling. Who wants to make me feel silly by explaining why this PowerShell code produces no output? No errors. Just 3 new blank lines.

Edit: I know what $_ is and what it's supposed to do. It's simply not working in this loop. Thx

Dr_Snooz
  • 1
  • 1
  • 4
    `$_` has a value only in a `ForEach-Object` pipeline. What you have here is a `foreach` loop. Use `Write-Host $e` -- you didn't declare that `$e` for nothing! – Jeroen Mostert Feb 02 '23 at 21:19
  • 1
    `foreach` and `ForEach-Object` are not the same, one is a language keyword the other is a cmdlet. `$_` is auto populated in scriptblocks that process the current object, such as in the pipeline. – Santiago Squarzon Feb 02 '23 at 21:23
  • Please read the `foreach` documentation. Not sure how more clear could we be. Jeroen's comment should be clear enough. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-7.3 – Santiago Squarzon Feb 03 '23 at 16:05

0 Answers0