The difference between $? and $ LastExitCode in PowerShell

In PowerShell, what is the difference between $? and $LastExitCode ?

I read about automatic variables , and he said:

$? Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.

$LastExitCode Contains the exit code of the last Windows-based program that was run.

In the definition of $? he does not explain what success and failure mean.




I ask because I suggested that $? is True if and only if $ LastExitCode is 0, but I found an unexpected counter example: $ LastExitCode = 0, but $? = Invalid in PowerShell. Redirecting stderr to stdout gives a NativeCommandError .

+39
command-line windows powershell
May 19 '12 at 2:28 pm
source share
1 answer

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

For cmdlets to fail, an exception is usually implied; for native applications, this is a nonzero exit code:

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

Canceling a cmdlet with Ctrl + C will also be considered an error; for native applications, it depends on which exit code they set.

+38
May 19 '12 at 14:30
source share



All Articles