Can anyone explain what's occurring in the case of select -first 0
example in the code below?
Function Test-Example {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
$InputObject
)
process {
$global:x++
write-verbose 'I''m running!'
$InputObject
}
}
[int]$global:x = 0 #reset the counter
1..100 | Test-Example -Verbose | select -first 10
$global:x #outputs 10
$global:x = 0 #reset the counter
1..100 | Test-Example | select -first 1000
$global:x #outputs 100; as we only iterate 100 times depsite asking for the first 1000
$global:x = 0 #reset the counter
1..100 | Test-Example | select -first 0
$global:x #outputs 100; which doesn't make sense since we don't see any output, suggesting `select -first 0` behaves like `select * | out-null`.
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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With