October 14th, 2006

Windows PowerShell Exit Codes

PowerShell Team
PowerShell Team

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

 

 

Category
PowerShell
Topics
FAQ

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.