Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Hey PowerShell Guy How can I run a PowerShell Script from CMD.EXE and return an Errorlevel

This is a translation from the Hey Scripting guy article : How Can I Compare Two Strings Values and Then Report Back the Result of That Comparison? ,

a quick translation to PowerShell :

PARAM ($Str1,$str2)

if ($str1.length -lt $str2.length){"string 1 length is less than string 2";exit 1}
if ($str1.length -eq $str2.length){"strings are equal";exit 2}
if ($str1.length -gt $str2.length){"string 1 length is greater than string 2";exit 3}

We save this in the file compare-string.ps1 and the result in PowerShell (the $LASTEXITCODE variable in PowerShell is what %errorlevel% is in CMD.exe ):

PoSH> .\compare-string.ps1 a ab
string 1 length is less than string 2
PoSH> $LASTEXITCODE
1
PoSH>.\compare-string.ps1 ab ab
strings are equal
PoSH> $LASTEXITCODE
2
PoSH> .\compare-string.ps1 ab a
string 1 length is greater than string 2
PoSH> $LASTEXITCODE
3

comparing strings is trivial in PowerShell end the exit command works almost the same wscipt.Quit(3),

but why I picked out this Atricle ( and adapted the title ) was because of the use of errorlevel , this implies using this from a Batch file in CMD.EXE and you need the errorlevel to return it to there, to that Batch file that is calling the PowerShell command from CMD.EXE.

if we try this from CMD.EXE in the following (most obvious) way's this seems not to work :

C:\Users\mvanorsouw>powershell -noprofile -file "compare-string.ps1" ab a
string 1 length is greater than string 2

C:\Users\mvanorsouw>echo %errorlevel%
0

C:\Users\mvanorsouw>powershell -noprofile -command ".\compare-string.ps1" ab a
string 1 length is greater than string 2

C:\Users\mvanorsouw>echo %errorlevel%
1

It is possible as we can do this :

C:\Users\mvanorsouw>powershell -noprofile -command "exit 10"

C:\Users\mvanorsouw>echo %errorlevel%
10

So why does this not work, the Problem here is that the exit from the script is not the one that does close the PowerShell console, only the script.

You can solve this by wrapping the calling of the PowerShell script in a PowerShell command to execute like this :

powershell -NoProfile -command ".\compare-string.ps1 a ab;exit $LASTEXITCODE"
echo %errorlevel%

now this script get's executed and then this result is used to exit PowerShell after the script is finished.

and the results from CMD.exe we can see here :

Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

C:\Users\mvanorsouw>powershell -NoProfile -command ".\compare-string.ps1 a ab;exit $LASTEXITCODE"
string 1 length is less than string 2

C:\Users\mvanorsouw>echo %errorlevel%
1

C:\Users\mvanorsouw>powershell -NoProfile -command ".\compare-string.ps1 ab ab;exit $LASTEXITCODE"
strings are equal

C:\Users\mvanorsouw>echo %errorlevel%
2

C:\Users\mvanorsouw>powershell -NoProfile -command ".\compare-string.ps1 ab a;exit $LASTEXITCODE"
string 1 length is greater than string 2

C:\Users\mvanorsouw>echo %errorlevel%
3

A bit inconvenient as we can not put the Parameters at the end of the Command this way, but it works and you could fix that again by another wrapper in the Batch file.

at least this method gives you a good way to fit in PowerShell into existing batch scripts as you use error levels in them.

For the -Command parameter this behavior seems logical, for the -File parameter (that takes a script as parameter) I personally consider this a bug on PowerShell.

Enjoy,

Greetings /\/\o\/\/

Published Tuesday, May 20, 2008 6:24 PM by MoW
Filed under: ,

Comments

# Monitor web-site availability « Dmitry’s PowerBlog: PowerShell and beyond

# PowerShell script in a .bat file « Dmitry’s PowerBlog: PowerShell and beyond

Anonymous comments are disabled