---
title: "How to Use the PowerShell Exit Command and Friends"
description: "Did you ever wonder in how many ways you can use the PowerShell exit command to exit a session or script? Wonder no more! Dive in to learn more."
canonical: "https://adamtheautomator.com/powershell-exit/"
---

# How to Use the PowerShell Exit Command and Friends

> Did you ever wonder in how many ways you can use the PowerShell exit command to exit a session or script? Wonder no more! Dive in to learn more.

Source: https://adamtheautomator.com/powershell-exit/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Use the PowerShell Exit Command and Friends](https://adamtheautomator.com/wp-content/uploads/2021/09/How-to-Terminate-a-PowerShell-Script-All-the-Ways.jpg)

# How to Use the PowerShell Exit Command and Friends

[![](https://secure.gravatar.com/avatar/4147968aa2332aa682bcebf295e4e9d0eb2672dee3d9ae0523a00ac51e7d6017?s=192&d=mm&r=g)Bill Kindle](https://adamtheautomator.com/author/bill/)28 September 20213 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[PowerShell](/tag/powershell/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Terminating the Console and Scripts with PowerShell Exit Command](#terminating-the-console-and-scripts-with-powershell-exit-command)
*   [Receiving and Providing Feedback with Exit Codes](#receiving-and-providing-feedback-with-exit-codes)
*   [Breaking Out of Loops With Break](#breaking-out-of-loops-with-break)
*   [Redirecting Code Execution with Return](#redirecting-code-execution-with-return)
*   [Conclusion](#conclusion)

Have you ever wondered, “What is the best way to terminate a script or exit a command in PowerShell?” Will the PowerShell session close with the PowerShell exit command? How to return custom exit codes? Well, you are in luck!

In this guide, you’ll have these questions and more answered. Learn about all the ways to exit a PowerShell script or session.

Let’s get ready to terminate some PowerShell sessions and scripts!

## Prerequisites

To follow along, be sure to have Windows PowerShell or PowerShell Core installed on your machine. The examples in this article will use [PowerSh](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell?view=powershell-7.1)[ell 7.1.4](https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell?view=powershell-7.1).

## Terminating the Console and Scripts with PowerShell Exit Command

Let’s dig into this tutorial and first cover the `exit` keyword, one of the many [reserved words](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_reserved_words?view=powershell-7.1) in PowerShell. A reserved word is a set of characters built into PowerShell that performs some action. For the `exit` keyword, the action is making PowerShell exit a PowerShell session.

Let’s practice! Open a PowerShell console session, type `exit`, and press the Enter key. The PowerShell console will immediately close.

This keyword can also exit a script rather than the console session. Including the `exit` keyword in a script and `exit` terminates only the script and not the entire console session from where the script runs.

Copy and paste the code below into notepad and save the script as _test.ps1_ to the _C:\\Temp_ directory. Run the PowerShell script file by running the command `.\text.ps1` as shown in the below screenshot. Notice that instead of PowerShell closing the session, PowerShell terminates the script and returns control to the prompt of the current PowerShell session.

```powershell
# test.ps1
Write-Host "Quick! Read this before the PowerShell session closes!!"
Start-Sleep -Seconds 2
Exit
```

![The exit keyword stopped the script execution, but the console is still running](https://adamtheautomator.com/wp-content/uploads/2021/09/image-212.png)

The `exit` keyword stopped the script execution, but the console is still running

## Receiving and Providing Feedback with Exit Codes

The `exit` command is helpful for situations when you need a script or function to run some code and exit. But you are not getting any indication if the script ran successfully or not.

When PowerShell runs the last command in a script, it stores the exit code of that last command in the [`$LASTEXITCODE`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.1#lastexitcode) variable.

There are two default exit codes:

*   0 – meaning success: normal termination
*   1 – meaning failure: [an uncaught throw](https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-exceptions?view=powershell-7.1#throw-and-catch)

PowerShell does not restrict you to `0` and `1` as exit codes. PowerShell lets you return custom exit codes, as long as they are [integers](https://docs.microsoft.com/en-us/powershell/scripting/lang-spec/chapter-04?view=powershell-7.1#423-integer).

> _The range of values for the [exit code](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_language_keywords?view=powershell-7.1#exit) is platform-dependent. On Windows, any 32-bit signed integers are allowed. On Unix, only positive 8-bit integers are allowed._

To exit PowerShell with a custom exit code, you can provide the exit code as an argument for the exit keyword.

Copy and paste the code below in the existing _test.ps1_ file and run the script with the command `.\Test.ps1`. Check the content of the `$LASTEXITCODE` variable. By telling PowerShell to exit with a `55` exit code, it did so, as you can see below.

```powershell
Function foo {
    Write-Output "This foo function will be shown"
    Exit 55 # < -- Using custom exit code
}

foo
```

![Returning a custom exit code](https://adamtheautomator.com/wp-content/uploads/2021/09/image-213.png)

Returning a custom exit code

## Breaking Out of Loops With Break

There will be moments where the `exit` behavior is not acceptable: like quitting a function. Let’s check how the `break` keyword helps.

The `break` keyword makes PowerShell stop a loop. After a `break`, the code continues executing at the next iteration.

Breaks are helpful in PowerShell loops such as the [foreach](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-7.1), [while](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_while?view=powershell-7.1), [do-while](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_do?view=powershell-7.1), and [switch statements](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_switch?view=powershell-7.1) because you can control where your code will halt before your code completes.

Let’s practice breaking out of loops. Below you can see a snippet code with a for loop. The for loop would iterate ten times if not for the `break` within. The `break` keyword will be triggered when the iterator is equal to eight. Once PowerShell finds the `break` keyword, it stops the loop.

```powershell
for($i=1; $i -le 10; $i++) {
	if($i -eq 8) { break }
	$i
}
```

You can see below that PowerShell stops the loop when `i` equals 8. The code never goes to 9. You have successfully broken out of a for loop.

![Breaking a for loop](https://adamtheautomator.com/wp-content/uploads/2021/09/image-214.png)

Breaking a for loop

Related:[Back to Basics: The PowerShell For Loop](https://adamtheautomator.com/powershell-for-loop/)

## Redirecting Code Execution with Return

The `return` keyword is slightly different from the previous keywords as it doesn’t exit or break commands. This keyword redirects code execution. It returns execution to its call while allowing you to return values. And not just integers, like `exit`, mind you. You can return any type! These behaviors make the `return` keyword more versatile than the previous keywords.

Related:[Your Getting Started Guide to Powershell Functions](https://adamtheautomator.com/powershell-functions/)

Let’s see it in action with an example: building a function to pair words. Copy the code below into your [PowerShell](https://adamtheautomator.com/powershell-functions/) session:

```powershell
function WordPairing ($a,$b) {

	return "$a" + "$b" # <-- Combining two words into one word

}

$output = WordPairing taco cat

Write-Output "$output is a strange pairing of words."
```

Notice how the value you are returning from the function in the output.

![Redirecting code execution with the return keyword](https://adamtheautomator.com/wp-content/uploads/2021/09/image-215.png)

Redirecting code execution with the `return` keyword

You redirect the `$output` generated by the `WordPairing` function back to where the `$output` is called.

## Conclusion

In this guide, you’ve learned different ways to terminate PowerShell sessions and scripts. Each method relies on a specific keyword. Each keyword has a particular use case. When used appropriately, each of these keywords gives you enormous control over what your script does.

Now that you know different ways for exiting your PowerShell sessions, how do you plan to improve your scripts with this newfound knowledge?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-exit%2F&text=How%20to%20Use%20the%20PowerShell%20Exit%20Command%20and%20Friends)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-exit%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpowershell-exit%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/06/26995-troubleshoot-dns-issues-powershell-codex.webp)

### [Troubleshoot DNS Issues with PowerShell](/troubleshoot-dns-issues-powershell/)

Troubleshoot DNS issues with PowerShell by testing name resolution, DNS client settings, cache entries, and network connectivity in a repeatable workflow.

![](https://adamtheautomator.com/wp-content/uploads/2025/10/image_2025-10-24_095322075.png)

### [Migrating from PowerShell 6 to 7.5: Breaking Changes/New Features](/migrating-powershell-6-to-7-5/)

Migrate from PowerShell Core 6 to 7.5: breaking changes, features, and testing tips.

![](https://adamtheautomator.com/wp-content/uploads/2025/06/featured-image-6.png)

### [How to Add Timeouts to Pester Tests with PowerShell Runspaces](/pester-test-timeout-runspaces/)

Prevent Pester tests from hanging indefinitely using PowerShell runspaces. Learn to handle variable scoping, module loading, TestDrive access, and stream capture challenges with timeout protection.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
