ChatGPT解决这个技术问题 Extra ChatGPT

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.

If you want to avoid closing the PowerShell window or ISE in my case; use "return" instead. It just ends the current running context. "New guy" thoroughly explains all options for educational purposes; you might want to consider changing your accepted answer (currently has more up votes anyway) for future StackOverflow users. It will allow you to debug the script as well.
Does this answer your question? What exactly is "exit" in PowerShell?

N
New Guy

I realize this is an old post but I find myself coming back to this thread a lot as it is one of the top search results when searching for this topic. However, I always leave more confused then when I came due to the conflicting information. Ultimately I always have to perform my own tests to figure it out. So this time I will post my findings.

TL;DR Most people will want to use Exit to terminate a running scripts. However, if your script is merely declaring functions to later be used in a shell, then you will want to use Return in the definitions of said functions.

Exit vs Return vs Break

Exit: This will "exit" the currently running context. If you call this command from a script it will exit the script. If you call this command from the shell it will exit the shell. If a function calls the Exit command it will exit what ever context it is running in. So if that function is only called from within a running script it will exit that script. However, if your script merely declares the function so that it can be used from the current shell and you run that function from the shell, it will exit the shell because the shell is the context in which the function contianing the Exit command is running. Note: By default if you right click on a script to run it in PowerShell, once the script is done running, PowerShell will close automatically. This has nothing to do with the Exit command or anything else in your script. It is just a default PowerShell behavior for scripts being ran using this specific method of running a script. The same is true for batch files and the Command Line window.

Return: This will return to the previous call point. If you call this command from a script (outside any functions) it will return to the shell. If you call this command from the shell it will return to the shell (which is the previous call point for a single command ran from the shell). If you call this command from a function it will return to where ever the function was called from. Execution of any commands after the call point that it is returned to will continue from that point. If a script is called from the shell and it contains the Return command outside any functions then when it returns to the shell there are no more commands to run thus making a Return used in this way essentially the same as Exit.

Break: This will break out of loops and switch cases. If you call this command while not in a loop or switch case it will break out of the script. If you call Break inside a loop that is nested inside a loop it will only break out of the loop it was called in. There is also an interesting feature of Break where you can prefix a loop with a label and then you can break out of that labeled loop even if the Break command is called within several nested groups within that labeled loop. While ($true) { # Code here will run :myLabel While ($true) { # Code here will run While ($true) { # Code here will run While ($true) { # Code here will run Break myLabel # Code here will not run } # Code here will not run } # Code here will not run } # Code here will run }


Also worth noting that Exit can take a return code as a parameter (defaults to 0) - e.g. Exit 3.
I agree, this is a much better answer. "Break" can have unintended consequences.
@BillK Indeed. I have updated the answer. When I first wrote this I remember not finding any official documentation on Exit. I then did a Get-Command Exit and Get-Alias Exit with no results. I then looked to see if it was a keyword and found no official documentation on it. Looking at it now however, I do not know how I came to that conclusion. Clearly it is a keyword (technet.microsoft.com/en-us/library/hh847744.aspx). Perhaps because Exit is one of the only keywords that doesn't have its own about_ help topic and therefore the list of topics in the left sidebar didn't include it.
What about Throw?
Is it applicable to return 0, 1, or 2, to indicate traditional error codes a la exit?
S
StackzOfZtuff

You should use the exit keyword.


How is this the accepted answer? It does specifically "closes the console window" which the asker said "which is not what I want."
@claudekennilol Only the question asker can accept an answer. Either they missed the fact that exit is supposed to close the window, or they changed their mind and deemed that acceptable for their purpose.
It does not close the console window in v3 or v4, which I use. Well, it will if you run the script from explorer, but no matter how you end the script it will do that. If running from a PowerShell command window it does not close the window.
Consider adding an error code if you're exiting due to abnormal circumstances. Like Exit -1 or Exit 1. Any number other than 0 resolves $? to False.
@claudekennilol It doesn't exit the console window when you run the script from a console. This behaves similarly to python's sys.exit, as was asked. $host.SetShouldExit() closes the console window when running a script from the console. What he didn't want. This seems to have the desired behavior to me...
P
Peter Mortensen

Exit will exit PowerShell too. If you wish to "break" out of just the current function or script - use Break :)

If ($Breakout -eq $true)
{
     Write-Host "Break Out!"
     Break
}
ElseIf ($Breakout -eq $false)
{
     Write-Host "No Breakout for you!"
}
Else
{
    Write-Host "Breakout wasn't defined..."
}

Please don't use break when wanting to just break out of the current function... calling scripts can be disrupted as well!
C
Community

Write-Error is for non-terminating errors and throw is for terminating errors

The Write-Error cmdlet declares a non-terminating error. By default, errors are sent in the error stream to the host program to be displayed, along with output. Non-terminating errors write an error to the error stream, but they do not stop command processing. If a non-terminating error is declared on one item in a collection of input items, the command continues to process the other items in the collection. To declare a terminating error, use the Throw keyword. For more information, see about_Throw (http://go.microsoft.com/fwlink/?LinkID=145153).


This seems the most-correct way to terminate an activity with an error. It delegates to the caller to attempt to handle the error, which is much better than simply attempting to terminate abruptly.
For me, at least in module functions, throw exits but doesn't set an exit code. This was executed via CLI e.g. powershell -command "& module-function ...". I needed to convert those functions to throw to a wrapping try-catch and exit from that wrapping catch in order to actually output an error exit code.
Don't forget $PSCmdlet.ThrowTerminatingError() for those instances when throw just can't get the job done (known issue with none terminating errors from throw)
g
gabriwinter

Terminates this process and gives the underlying operating system the specified exit code.

https://msdn.microsoft.com/en-us/library/system.environment.exit%28v=vs.110%29.aspx

[Environment]::Exit(1)

This will allow you to exit with a specific exit code, that can be picked up from the caller.


In PowerShell, you can simply use the built-in Exit for this, e.g. Exit 1.
built-in Exit won't always work as you expect. Try this in powershell: "Exit 5" > test1.ps1 powershell.exe .\test1.ps1 $lastexitcode "[Environment]::Exit(5)" > test2.ps1 powershell.exe .\test2.ps1 $lastexitcode
[Environment]::Exit(1) has the side effect of exiting my powershell window when I invoke it in a script, where as using exit doesn't appear to do so.
P
Peter Mortensen

Throwing an exception will be good especially if you want to clarify the error reason:

throw "Error Message"

This will generate a terminating error.


I don't think this adds any more than Greg Bray's answer nearly a year earlier stackoverflow.com/a/20556550/695671
I used this for to, e.g.: Throw "The current value of my variable: $MyVariable", it was very helpful to me
R
Rob

I think you are looking for Return instead of Break. Break is typically used for loops and only breaks from the innermost code block. Use Return to exit a function or script.


This is not correct--the OP specifically asks for exiting a script from within a function. Return will simply return from the function, not from the script. (Return at the top level of a script will terminate the script, but that was not the question.)
was this 'answer' actually comments on EverydayNerd answer?
@tkokasih Perhaps it was intended as a comment but the way it came out, it's an excellently kick-ass answer.Not only does it address the actual question (making it an answer). It also provides additional relevants (hence excellency). And above all, it does it in two lines of text (definitely kickass'y, if yet understandably easy to mistake as a comment). Would you agree?
i
iRon

I coincidentally found out that Break <UnknownLabel> (e.g. simply Break Script, where the label Script doesn't exists) appears to break out of the entire script (even from within a function) and keeps the host alive.
This way you could create a function that breaks the script from anywhere (e.g. a recursive loop) without knowing the current scope (and creating labels):

Function Quit($Text) {
    Write-Host "Quiting because: " $Text
    Break Script
} 

Your answer is true, but be aware this can cause problems for callers of your script if they disagree with your desire to exit the entire script: stackoverflow.com/questions/45746588/…
f
fpschultze

May be it is better to use "trap". A PowerShell trap specifies a codeblock to run when a terminating or error occurs. Type

Get-Help about_trap

to learn more about the trap statement.


M
Michael Meli

I used this for a reruning of a program. I don't know if it would help, but it is a simple if statement requiring only two different entry's. It worked in powershell for me.

$rerun = Read-Host "Rerun report (y/n)?"

if($rerun -eq "y") { Show-MemoryReport }
if($rerun -eq "n") { Exit }

Don't know if this helps, but i believe this would be along the lines of terminating a program after you have run it. However in this case, every defined input requires a listed and categorized output. You could also have the exit call up a new prompt line and terminate the program that way.


V
Vopel

I thought up a neat little trick to do just that, when I wanted a function to exit a script without throwing an error, but not exit the console if the function was used from there. It involves the $PSScriptRoot automatic variable that is only defined when running a script.

if($PSScriptRoot){exit}