Difference between $? and $LastExitCode in PowerShell

后端 未结 1 1051
别跟我提以往
别跟我提以往 2020-12-01 13:15

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

I read about automatic variables, and it said:

相关标签:
1条回答
  • 2020-12-01 14:22

    $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.

    0 讨论(0)
提交回复
热议问题