Difference between $? and $LastExitCode in PowerShell

$LastExitCode is the return code of native applications. $? just returns True or False depending on whether the last command (cmdlet or native) exited without error or not.

For cmdlets failure usually means an exception, for native applications it’s a non-zero exit code:

PS> cmd /c "exit 5"
PS> $?
False
PS> cmd /c "exit 0"
PS> $?
True

Cancelling a cmdlet with Ctrl+C will also count as failure; for native applications it depends on what exit code they set.

Leave a Comment