Export (0) Print
Expand All
Expand Minimize

Converting the Windows Script Host Quit Method


Windows PowerShell


Definition: Forces script execution to stop at any time.

Quit


This was a tough one for the Scripting Guys; needless to say, we don’t even know the meaning of the word “Quit.” (Needless to say, we don’t even know the meaning of a lot of words. But that’s another story.) As it turns out, though, the WSH method Quit provides a way to exit a script. Need to exit a script? Then just call the Quit method:


Wscript.Quit


But wait, that’s not all. In addition to terminating a script, the Quit method enables you to specify an exit code when terminating that script. For example, this line of code terminates a script with an exit code of 32:


Wscript.Quit (32)


Why would you even want to specify an exit code when terminating a script? Well, for one thing, that enables you to easily determine why a script terminated, something you can do using this simple command-line command:


echo %errorlevel%


If you specify different exit codes for different situations (0 indicates that the script successfully completed its task; 1 indicates that a file could not be found; 2 indicates that the drive was not ready; etc.) you’ll never be left guessing why your script quit, or whether or not it succeeded.

Windows PowerShell provides the same capability via the Exit command. Need to terminate a script? Okey-doke:


exit


Need to specify an exit code when terminating that script? That’s fine, too; just specify an exit code when terminating the script:


exit 32


You can use the $LastExitCode variable to report back this exit code:


$LastExitCode



See conversions of other Windows Script Host methods and properties.
Return to the VBScript to Windows PowerShell home page

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft