Welcome to MSDN Blogs Sign in | Join | Help

ErrorLevel equivalent

PSMDTAG:FAQ: ErrorLevel - what is the PowerShell equivalent?

In Cmd.exe, %ErrorLevel% is a builtin variable which indicates the success or failure of the last executable run.

In PowerShell, we support:

    $?
       Contains True if last operation succeeded and False otherwise.
And

    $LASTEXITCODE
       Contains the exit code of the last Win32 executable execution.

PS> 2+2
4
PS> $?
True
PS> 3/$null
Attempted to divide by zero.
At line:1 char:3
+ 3/$ <<<< null
PS> $?
False
PS>
PS> ping localhost

Pinging jpsvista1.ntdev.corp.microsoft.com [::1] from ::1 with 32 bytes of data:

Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms

Ping statistics for ::1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms
PS> $lastexitcode
0
PS> ping asdfasdf
Ping request could not find host asdfasdf. Please check the name and try again.
PS> $lastexitcode
1

 

Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

 

PSMDTAG:ERROR: %ErrorLevel% , $? and $LastExitCode

Published Friday, September 15, 2006 5:04 AM by PowerShellTeam
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: ErrorLevel equivalent

I was trying to see if it is possible to capture both "$LASTEXITCODE" and "$?" at the same time since getting the value of "$LASTEXITCODE" first would reset the value of "$?".

My approach was to assign array of @($LASTEXITCODE, $?) to two different variables, say, $a & $b.

I had two demo functions
function foo { ping a }
function bar { throw "error!" }

After running

foo | bar

I ran

$a, $b = @($LASTEXITCODE, $?)

Values of
$a -> 1
$b -> False

The result seemed correct but I am not certain if "$a, $b = @($LASTEXITCODE, $?)" is the right way to capture both error values or not since "$?" is the second element in the array and not sure if anything is going on to change the value of last powershell error value($?) while assigning array values to two different variables.

Am I just too paranoid?
Friday, September 15, 2006 12:43 PM by Sung M Kim

# re: ErrorLevel equivalent

$? always reflects the status of the last expression.  This is highlighted by the following example:

PS> function barf {throw "Spew"}
PS> barf
Spew
At line:1 char:21
+ function barf {throw  <<<< "Spew"}
PS> $?
False
PS> $?
True

$LastExitCode changes only when you run an executable.  As such, just grab $? first and then grab $LastExitCode - you don't need to get them in a subexpression.

Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Friday, September 15, 2006 6:16 PM by PowerShellTeam

# re: ErrorLevel equivalent

$LASTEXITCODE would be a good name for a film about a burnt out techie who writes a powershell script that embezzles trillions of dollars out of a bank.
Tuesday, October 03, 2006 4:09 AM by lb

# Read only property?

Can we change the value in lastexitcode ?

is this a read only attribute

Monday, January 07, 2008 9:02 PM by Manju

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker