How to stop PowerShell from closing after running a script

Vlad Turiceanu
by Vlad Turiceanu
Editor-in-Chief
0 Comments
Download PDF
Affiliate Disclosure

keep PowerShell window open

PowerShell is both a command-line shell and a scripting language. There’s really no surprise that it’s preferred by many users who have tasks that they want to run automatically to manage operating systems and their processes.

One of their often requirements is to stop PowerShell from closing after running a script. They wish to run a PowerShell script and make the window stay open after it finishes running. This is a must for remotely log in to another server using the requested credentials when they run the script.

So, without further ado, let’s discuss some quick tips for keeping a PowerShell window open after the script finishes running.

How can I stop PowerShell from closing after running a script?

1. Initiate a remote connection via these steps

initiate remote connection

  1. You need to create a file called execute.ps1 on the desktop.
  2. Paste this content in it:
    clear-Host
    $br = “`n”;$br
    Write-Output ‘Step1: Press 1 for Powershell to reload’
    Write-Output ‘Step2: Press 2 to start remote session’
    $br
    Write-Host ‘This script will’ -NoNewline -ForegroundColor Green
    Write-Host ‘ not ‘ -ForegroundColor Red -BackgroundColor Black -NoNewline
    Write-Host ‘indicate which step you are on,’ -ForegroundColor Green
    Write-Host ‘if you are not sure, rather press 1 again and then 2.’ -ForegroundColor Green
    $br
    $input = Read-Host ‘Enter your choice’
    switch($input){
    # Change the name of the execute.ps1 if you want to
    1{powershell.exe -noexit “C:users$env:usernameDesktopexecute.ps1” -noprofile}
    #
    2{
    Clear-Host;Write-Warning ‘You are about to connect to the remote server’
    # Change the ‘RemoteServerName’ to the name of your server and change the ‘domainname’ to your actual domain name
    Enter-PSSession -ComputerName RemoteServerName -Credential “domainname$env:username”
    }
    default{Write-Warning ‘An error has occurred’}
    }
  3. Run it. The first time you do that, press 1 to open it up again with the ‘no exit’ switch.
  4. The second time, press 2 to initiate the remote connection to the remote computer.

2. Use the Pause command

Pause command

Why not giving a try to the Pause command at the end of execution? It will immediately cause a new command interpreter to start and run in the background.

This makes the PowerShell window wait until you decide to further press the Enter key, so go for it:
cmd /c pause


3. Add a line to the end of your script

add a line to the end of your script

Another quick, yet effective solution is to add a line like this to the end of your script:
Read-Host -Prompt “Press Enter to exit”

The effect is guaranteed and it works in PS-ISE. However, be careful that it accepts only the Enter key.


4. Execute a PowerShell script locally to run on a remote computer

PowerShell script

You may also try to create an open.ps1 like the one you’re seeing here:
powershell.exe -noexit c:remote.ps1

This calls the remote.ps1:
Enter-PSSession -ComputerName YourExchangeServer -Credential Get-Credential


5. Use the Start-Sleep cmdlet

use Start-Sleep cmdlet

When faced with the problem of looking for a way to stop PowerShell from closing after running a script, another effective, yet underrated PowerShell cmdlet is this one:
Start-Sleep -s 15

For those of you who don’t know, this Start-Sleep cmdlet or the sleep alias basically suspends the activity in a script for the specified period of time. You can use it for plenty of other tasks, such as pausing before repeating an operation or waiting for an operation to complete.

It’s up to you to decide if it’s good enough for your case. Just remember to use the Ctrl+C keyboard hotkey when you’re ready to break out of Start-Sleep.


There are plenty of uses for PowerShell when you have a lot of processes that need to run under certain conditions and you lack the time or patience to wait for each process to finish in order to start the next.

In this case, you have to prevent PowerShell from closing after running a script and you’ve seen that it’s possible. You can use any of the procedures described above according to your needs and convenience.

Let us know which one worked for you via the comments area below.

READ ALSO:

Was this page helpful?
Thanks for letting us know! You can also help us by leaving a review on MyWOT or Trustpillot.
Get the most from your tech with our daily tips
Tell us why!