Windows PowerShell Exit Codes

Windows PowerShell Exit Codes

  • Comments 10

Windows PowerShell Exit Codes

 

PSMDTAG:FAQ: How can my script control the PowerShell exit code?

Answers:

1.      A normal termination will set the exitcode to 0

2.      An uncaught THROW will set the exitcode to 1

3.      The EXIT statement will stop the process and set the exitcode to whatever is specified.

 

c:\>powershell -noprofile -command "Write-output Test"
Test

c:\>echo %errorlevel%
0

c:\>powershell -noprofile -command "Write-output Test; throw 'ERROR'"
Test
ERROR
At line:1 char:25
+ Write-output Test; throw  <<<< 'ERROR'


c:\>echo %errorlevel%
1

c:\>powershell -noprofile -command "Write-output Test; exit 31492"
Test

c:\>echo %errorlevel%
31492

 

Jeffrey Snover [MSFT]
Windows PowerShell/MMC 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

 

 

Leave a Comment
  • Please add 1 and 8 and type the answer here:
  • Post
  • I love the new code markup---one issue though: in IE7 RSS reader the markup CSS is visible at the top of the story, in a huge block. As a web page the blog entry appears fine however.

  • I was wondering how can I check exit code from a PS script.

    # filename: tp.ps1

    $s = get-service | select-object Name, status

    $b = $False

    foreach ($i in $s)

    {

           # Check foe Exchange store service.

           # Misspelled service name.

    if ( $i.name -eq "MSExchangeI")

    {$b = $True}

    }

    if ($b -eq $True)

    {exit 0}

    else

    {exit 99}

    # tp.ps1 ends

    I called this script from cmd.exe as follows.

    Powershell C:\ts.ps1

    echo %errorlrvel%

    This prints 1 to console and not 99. What am I doing wrong here?

  • Hi

    I have the same problem.....

    Have anyone found an solution for this problem???

  • Try running the script like this:

    PowerShell –Command “& { c:\ts.ps1; exit $LASTEXITCODE}”

    echo %errorlevel% - Should show what the ps1 script exited with.

  • Check out Get-ExitCode.ps1 on

    http://huddledmasses.org/jaykul/register-dll-calling-regsvr32-without-messageboxes-in-powershell/

  • How would you extend this example to executing commands on a remote server with Powershell 2.0? I'm trying to get the exit code of a remotely executed command.

    Example: I want to run the command...

    c:\windows\system32\inetsrv\appcmd.exe start site MyWebsite

    ...on the server, MyServer.

    Ive been trying something like this but it doesnt seem to quite work...

    powershell -noprofile -command "icm MyServer {c:\windows\system32\inetsrv\appcmd.exe start site MyWebsite; exit $lastexitcode}"

    I've also tried creating a new session with new-pssession but that didnt seem to work either.

    Thanks

    CT

  • I'm not sure if it's intended but the following inconsistency caused me no end of grief as I started using throw as my default error style along with the -file switch for invoking via a batch file.

    PowerShell -command "throw 'error'"

    ...returns 1 as the exit code - good. Now put the 'throw' into a .ps1 file, e.g. test.ps1

    throw 'error'

    ...and run with the -file switch:-

    PowerShell -file .\test.ps1

    ...it now returns 0 as the exit code. But, if you run the script with the -command switch:-

    PowerShell -command .\test.ps1

    ...it correctly returns 1. So why is there a difference between using the -file and -command switches to invoke a script?

  • Hi there.

    I'm getting -196608 as exit code of my C# program. The line of code that starts the process is:

    Process.Start(new ProcessStartInfo() { Verb = "runas", UseShellExecute = true, FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe", Arguments = @"-file C:\SharePoint Backup Scripts\backupsharepointfarm.ps1" })

    It works (returns 0) when there are no arguments.

    Any clues will be aprecciate :-)

  • Hi Kaleu, have you got your opening " in the wrong place before the -file parameter? i.e. it should be -file "C:\SharePoint Backup Scripts\backupsharepointfarm.ps1" not "-file C:\SharePoint Backup Scripts\backupsharepointfarm.ps1"

  • #This is a comment.

    Write-Output "Hello, World!"

    $NAME=Read-Host "What is your name, ugly?"

    Write-Output "You're ugly, $NAME"

    [int] $N = Read-Host "Enter a number between 1 and 10"

    while ($N -lt 0 -or $N -gt 10)

    {

    $N = Read-Host "Dude.  I was pretty specific. Try again:"

    }

    [char] $N = Read-Host "Now, try a number between 1 and 9"

    switch($N)

    {

    2 {Write-Output "Prime!"}

    3 {Write-Output "Prime!"}

    5 {Write-Output "Prime!"}

    7 {Write-Output "Prime!"}

    4 {Write-Output "Square!"}

    9 {Write-Output "Square!"}

    8 {Write-Output "Cube!"}

    default {Write-Output "Boring."

    Write-Output "You need to learn to pick better numbers."}

    }

    for( $i = 0; $i -lt 4; $i++)

    {

       for($j = 0; $j -le $i; $j++)

       {

           Write-Host -NoNewLine "*"

       }

       Write-Host ""

    }

    foreach ($X in "Larry","Moe","Curly")

    {

    Write-Output "$X is a stooge."

    }

    $NUMARGS = $args.count

    Write-Host "There are $NUMARGS arguments."

    Write-Host "Here are the arguments..."

    foreach( $ARG in $args )

    {

    Write-Host $ARG

    }

Page 1 of 1 (10 items)