Skip to end of metadata
Go to start of metadata

Exit Code Handling with PowerShell

The exit code of a PowerShell script is usually expected as the result of the execution of the script. However, the program powershell.exe returns the exit code of the execution of the powershell.exe itself - and this in most cases 0.

To retrieve the effective exit code of the script, that script has to be terminated with an exit() function and as parameter the variable $lastexitcode (or any other value/variable for the exit code).

The example below shows how it works:

 

The result of this job is visible in the log file:

The statement exit %errorlevel% passes the exit code to JobScheduler. If this statement is missing then JobScheduler will not know about the exit code and will assume that it is zero (0).

See also