PowerShell: Tips for terminating code execution

- select the contributor at the end of the page -
Test your skills. Learn something new. Get help. Repeat. Start a FREE 10-Day trial
Nothing is ever straightforward when talking about code. Ask any 10 developers (or scripters) how to do something, and you're bound to get 10 different answers that accomplish the same thing. The answer always depends on how and where the code is executed, what needs to execute afterward (if anything) and what the dependencies are, among other factors. And the same goes for PowerShell code.

PowerShell isn't necessarily a programming language, but it exhibits many of the same characteristics. Take code execution termination, for example. Sometimes, a PowerShell script needs to stop execution, or terminate dead in it's tracks. Perhaps the script blew up and was so catastrophic that the error couldn't be recovered. Or maybe it's a less complicated matter, like when the necessary criteria to run the script isn't met and it's just not useful anymore. Simply put, there are many reasons to terminate code execution before it's run it's full course.


Terminating “code execution” can mean different things. It's important to note that when PowerShell code is stopped it doesn't necessarily mean that the entire script stops. PowerShell contains many different scopes. Think of a scope as a barrier that prevents certain code from seeing other code (all code inside each scope is it's own entity). Scopes are also created in a hierarchy, so a parent scope can contain multiple child scopes, for example. Some methods to terminate code execution simply terminate the scope and not the entire script.

There are many different ways to stop code execution from running lesser known commands like [Environment]::Exit(1), $host.SetShouldExit() and $host.Exit(). But, overall, they accomplish the same thing. These are the nuclear ways to stop code execution; they simply exit the current PowerShell host.

Think of it like this: The PowerShell console itself is a host. If you run one of these commands directly in the console, it will exit the console. Likewise, if you put one of these commands in a script or even in a function in a script, the entire host will exit. This is the nuclear approach and, you may have already guessed that it isn't used very often.

There are three more well-known options for terminating PowerShell code execution including exit, return and break. Let's take a more in-depth look at these, and when you might want to use them in a script.


The exit keyword is used to exit from contexts; it will exit the (currently running) context where your code is running. This means that if you use this keyword in a script, and launch the script directly from your console, it will exit both the script and the console since they're both running in the same context. However, if you use the exit keyword in a function within the script, and call that function from the same script, it will just exit the script and not the entire console. The exit keyword is best used in functions, and when those functions are called in a script. It's a great way to terminate execution of functions.


The return keyword doesn't necessarily terminate code execution, so much as it redirects it. The return keyword is used to return code execution to the previous point in which it was called. For example, if return is simply executed from the shell console, nothing will happen because that's the point at which it was called. If return is used in a script, it will halt code execution (at that point in the script) and return to wherever it was called. If the script is called from the shell, it will return execution back to the shell.

The return keyword is very useful, but tracking it can get confusing if you've got layers and layers of functions. However, it's one of the most versatile options of redirecting code execution.


The break keyword differs from both return and exit, although it's most similar to return. Break is generally used in loops and switch statements. If used in this context, break will “break” out of a loop or a switch statement. This means that if a loop is processing all elements in a collection, and break is used, it will terminate the entire loop and no further elements will be processed.

Break can also be used outside of its typical constructs, and can simply be placed in a script outside of a loop. In this case, break will stop all script execution, even if called within a function in the script. It's a great way to ensure the entire script halts wherever it may be (excluding in a loop or switch statement).


As you can see, there are many ways to either stop or redirect code execution in a PowerShell script. But describing each method's behaviors in a simple blog post isn't as efficient as putting them into action, and that's why I highly suggest playing with these keywords. Insert them in different places inside your scripts and notice how your script behaves. Once you get a feel for how these keywords work, you'll have a far better handle on when you might need to use one.

Get our content first. In your inbox.

Loading form...

If this message remains, it may be due to cookies being disabled or to an ad blocker.


Adam Bertram

is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at his site listed below or on Twitter at @adbertram.