How to Use the PowerShell Exit Command

PowerShell uses the “Exit” command in its functions and scripts to terminate them. This command should be utilized carefully, as it can terminate the console and editors. The user might be unable to see the output when it is used inside the script or function. It is the easiest way to exit and terminate the script or console.

This write-up will discuss the “Exit” command in PowerShell.

How to Use the PowerShell “Exit” Command?

The “Exit” command closes the PowerShell console session or terminates a script in PowerShell. For the practical demonstration, check out the given examples.

Example 1: Exit PowerShell Console

Executing the “Exit” command in the PowerShell console will terminate the console and take exit from it:

> Exit


Example 2: Exit PowerShell Script

In this example, the script will get terminated with the help of the “Exit” command but it won’t terminate the console:

> Function ExitScript{
>    write-output "The console won't get terminated."
>    Exit
> }
> ExitScript


In the above code:

  • First, define a function and add a code inside. Add the “Exit” command at the end of the code.
  • Finally, invoke the function by calling its name outside its curly braces:

Now, let’s test the script by calling it in the PowerShell console:

> C:\New\ExitCommand.ps1


It can be seen that the PowerShell script got terminated, but it did not close the console session.

Example 3: Exit PowerShell Script After Five Seconds

This example will demonstrate taking a 5-second pause before terminating the script:

> write-Host "It will terminate after 5 seconds"
> Start-Sleep -seconds 5
> Exit



  • First of all, we have utilized the “Write-Host” command and added some text inside it.
  • After that, add the “Start-Sleep” command alongside the “-seconds” parameter and assign the “5” parameter to add the delay of 5 seconds.
  • Finally, specify the “Exit” command:

Let’s test the above script by calling it:

> C:\New\ExitCommand.ps1


The above-given script waited for 5 seconds before getting terminated.

Example 4: Checking If Code After the “Exit” Command Executes

Now, add the following code to your PowerShell script:

> write-Host "Hello World"
> Exit
> Write-Host "This won't get executed."


It can be observed that the “Exit” command terminated the script, and the code after the “Exit” command did not get executed.


The “Exit” command, when used inside the PowerShell script, will terminate it but won’t close the session. However, when it is used inside the PowerShell console, it gets terminated. Moreover, it also helps in stopping the execution of the script. This write-up has discussed several examples to give a better demonstration of the “Exit” command.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.