Almost all applications and utilities will set an exit code when they complete/terminate.
The exit codes that are set will vary according to the application/utility, in general a code of 0 (false) will indicate successful completion.
The exit codes set by resource kit utilities are not always consistent, they may vary between machines with different Service packs/Resource kit updates applied.
Some utilities will return negative numbers as an exit code.
In the CMD shell the exit code is made available via the %ERRORLEVEL% variable.
Writing to the %ERRORLEVEL% variable is not advised because that value will take precedence over the internal ERRORLEVEL.
You can make a batch file return a non-zero exit code by using the EXIT command.
To force an ERRORLEVEL of 1 to be set, run a small but invalid command like COLOR 00
Or to speciy a particular ERRORLEVEL run CMD and Exit : Cmd /C Exit /B 5
You should never attempt to write to the %ERRORLEVEL% variable.
In PowerShell $? contains True if last operation succeeded and False otherwise.
The exit code of the last Win32 executable execution is stored in the automatic variable $LASTEXITCODE
To read exit codes (other than 0 or 1) launch the PowerShell script and return the $LASTEXITCODE in a single line like this:
powershell.exe -noprofile C:\scripts\script.ps1; exit $LASTEXITCODE
“I’d rather wake up in the middle of nowhere than in any city on earth” ~ Steve McQueen
Robocopy exit codes