Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell "select -First 0" behaviour

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).

like image 751
JohnLBevan Avatar asked Oct 29 '22 06:10

JohnLBevan


1 Answers

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

like image 82
Prasoon Karunan V Avatar answered Nov 30 '22 21:11

Prasoon Karunan V