PowerShell live documentation

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.

  1. $local:ErrorActionPreference = "SilentlyContinue"
  2. Write-Error error
  3. Write-Output "outside of parentheses: `$? = $?"
  4. (Write-Error error)
  5. Write-Output "inside of parentheses: `$? = $?"
Stdout
outside of parentheses: $? = False
inside of parentheses: $? = True
outside of parentheses: $? = False
inside of parentheses: $? = False