%Errorlevel%

Almost all applications and utilities will set an exit code when they complete/terminate.

In the CMD shell the exit code is made available via the %errorlevel% variable.

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.

Batch files

You should never attempt to write to the %errorlevel% variable directly. When writing a batch file, you can make it return a non-zero exit code by using the EXIT command.

PowerShell

In PowerShell the exit code 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

Related:

Robocopy exit codes


© Copyright SS64.com 1999-2013
Some rights reserved