kprobst kprobst - 3 months ago 16
PowerShell Question

Terminating a script in PowerShell

I've been looking for a way to terminate a PowerShell (PS1) script when an unrecoverable error occurs within a function. For example:

function foo() {
# Do stuff that causes an error
$host.Exit()
}


Of course there's no such thing as
$host.Exit()
. There is
$host.SetShouldExit()
, but this actually closes the console window, which is not what I want. What I need is something equivalent to Python's
sys.exit()
that will simply stop execution of the current script without further adieu.

Edit: Yeah, it's just
exit
. Duh.

Answer

You should use the exit keyword.