HOWTO · PowerShell
How to Get Exit Code From Start-Process in PowerShell
This article will explain how to obtain the exit code after running a script with the start-process cmdlet.
On this page
To get a process exit code with PowerShell Start-Process, capture the process object with -PassThru and wait for it to finish with -Wait. Then read its ExitCode property:
$pwshPath = (Get-Process -Id $PID).Path
$workingDirectory = [System.IO.Path]::GetTempPath()
$process = Start-Process -FilePath $pwshPath -ArgumentList @('-NoProfile', '-Command', 'exit 7') -WorkingDirectory $workingDirectory -Wait -PassThru
"Exit code: $($process.ExitCode)"
Exit code: 7
This example starts the current PowerShell executable in a temporary working directory and asks the child process to return exit code 7. Using the current executable keeps the example independent of Windows-only programs such as cmd.exe; the explicit working directory also avoids relying on the caller’s current directory. Replace the executable and arguments with the program you need to run. -Wait makes PowerShell wait for completion, and -PassThru returns the process object so its ExitCode can be read. Exit code 0 conventionally indicates success; the meaning of any nonzero value is defined by the program.
Get the Exit Code With -Wait and -PassThru
Use both parameters when the next part of your script depends on the launched program’s result. -Wait controls when PowerShell continues, while -PassThru gives you the process object. Read ExitCode after the process finishes to get its final result.
Assign $process.ExitCode to $exitCode and compare it with 0 before choosing what your script should do. Adapt the condition to the program’s documented exit codes. A nonzero value does not have one universal meaning; it may indicate an error or another documented outcome.
Why $LastExitCode Does Not Report a Start-Process Exit Code
$LastExitCode is not populated by Start-Process. It reports the exit code from the most recently run native program invoked directly in the current PowerShell session. Starting a separate process with Start-Process does not make that process’s exit code available through $LastExitCode.
Use the ExitCode property on the process object returned by Start-Process -PassThru instead. If you invoke a native program directly, PowerShell updates $LastExitCode when that program finishes; that is a separate invocation path and does not retrieve the result of a process started with Start-Process.
Get the Exit Code From a Process Object With WaitForExit()
You can also omit -Wait, keep the process object, and call its WaitForExit() method before reading ExitCode. This is useful when you need to do other work before waiting. The method blocks until that process has exited.
The equivalent sequence is: start it with -PassThru, call WaitForExit(), and then read $process.ExitCode. Keep the process object associated with the process whose result you need; do not use $LastExitCode for this path. This alternative is described without a separate output transcript because the packet contains no execution evidence for it.
Conclusion
For a process launched with Start-Process, combine -PassThru with -Wait and read the returned object’s ExitCode property. Alternatively, call WaitForExit() on that object before reading the property. $LastExitCode applies to native programs invoked directly from PowerShell, not to a process launched through Start-Process.