Exit status variable $?
In PowerShell, the $?
variable represents the exit status of the previous
command. If it's true, the command succeeded. If it's false, the command
failed. However, you need to be careful if using the variable, since
enclosing a command in parentheses can reset $?
to true in PowerShell 6
and earlier.
$local:ErrorActionPreference = "SilentlyContinue"
Write-Error error
Write-Output "outside of parentheses: `$? = $?"
(Write-Error error)
Write-Output "inside of parentheses: `$? = $?"
2.x, 5.x, 6.x | 7.x |
---|---|
outside of parentheses: $? = False inside of parentheses: $? = True | outside of parentheses: $? = False inside of parentheses: $? = False |