Powershell “select -First 0” behaviour

Question:

Can anyone explain what’s occurring in the case of select -first 0 example in the code below?

If we add the -verbose switch we see that the value of $global:x matches the number of iterations according to the verbose output (i.e. we get 10 verbose messages in the first example, 100 in the second, and 100 in the third).

Answer:

Select-Object -First 0 or Select-Object -Last 0

Actually the cmdlets internally have a check for this exact scenario and intentionally outputs nothing.

the reason why you are seeing I'm running! for 100 times is Write-Verbose is in Porcess() block. All 100 items get processed and outputs nothing as the code internally skips on the check $this.First != 0 then Skipp

Source:

Powershell “select -First 0” behaviour by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply