Home > Back-end >  How do I check the errorcode of a `&` Powershell command?
How do I check the errorcode of a `&` Powershell command?

Time:04-02

In a Powershell script, I run a command like:

& "C:\...\myprogram.exe"

How do I check to see if myprogram.exe returned an errorcode?

CodePudding user response:

If you run it with the call operator (or without), check $LASTEXITCODE. It will always be the exit code of the last program you ran in the current session.

If you need to use Start-Process instead (such as needing to use -Wait to wait on a GUI application to complete), make sure to use -PassThru to store the process object as a variable, then check its ExitCode property. $LASTEXITCODE is not set in this case.

  • Related