Closely related: #4002
Normally, immediately after execution of an external program, $? and $LASTEXITCODE -eq 0 can be used interchangeably to test for success.
However, under the following conditions $? is invariably set to $false, even if $LASTEXITCODE contains 0 and therefore indicates success:
- If a
2> or *> redirection is involved
- and actual stderr output is produced by the external program.
Note that you generally cannot and shouldn't infer success vs. failure of an external program from the presence of stderr output - many CLIs use stderr to emit status information; only the process exit code should be consulted.
Steps to reproduce
Run the following on Windows, which produces both stderr and stdout output and reports 0 as the process exit code:
& { cmd /c 'nosuch & ver' *>$null; $?; $LASTEXITCODE } | Should -Be $true, 0
Expected behavior
The test should succeed.
Actual behavior
The test fails:
Expected @($true, 0), but got @($false, 0).
That is, $? was $false, even though $LASTEXITCODE was 0.
Environment data
PowerShell Core 7.0.0-preview.3
Windows PowerShell v5.1.18362.145