Why does piping an external application to Select-Object -First 1 set $LastExitCode to -1?
Select -First <int>
works by collecting the requisite amount of input (in your case just 1 string) - after which point it throws a special exception that forces the runtime to interrupt/stop all upstream commands - in the case of an external application like where.exe
, that means forcefully closing it.
So -1
is not a real exit code, it's simply the "unknown exit code" value PowerShell uses to indicate that "we'll never know, because I killed'em".