Share this article
Improve this guide
PowerShell Closes After Running Script: How to Stop it
Discover expert resolutions that worked for others!
3 min. read
Updated onOctober 4, 2023
updated onOctober 4, 2023
Share this article
Improve this guide
Read our disclosure page to find out how can you help Windows Report sustain the editorial teamRead more
Key notes
PowerShellis both a command-line shell and a scripting language. There’s no surprise that it’s preferred by many for tasks they want to run automatically. But some reported that PowerShell closes after running a script.
They wish to run a script and want PowerShell not to close immediately. This is a must for remotely logging into another server using the requested credentials when they run the script. Keep reading to find out the solutions!
How do I stop PowerShell from closing after running a script?
1. Initiate a remote connection
2. Use the Pause command
Why not try thePausecommand at the end of execution? It will immediately cause a new command interpreter to start and run in the background. All you have to do is execute the following command:cmd /c pause
This makes the PowerShell window wait until you decide to press the Enter key further.
3. 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”
This is guaranteed to work if PowerShell closes after running a script, and it works in PS-ISE. However, be careful that it accepts only theEnterkey.
4. Execute a PowerShell script locally to run on a remote computer
You may also try to create anopen.ps1, like the one you see 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
When faced with the problem of looking for a way to stopPowerShellfrom closing after running a script, another effective yet underrated PowerShell cmdlet:Start-Sleep -s 15
For those who don’t know, thisStart-Sleepcmdlet or the sleep alias suspends the activity in a script for a specified period. You can use it for many other tasks, such as pausing before repeating an operation or waiting to complete it.
It’s up to you to decide if it’s good enough for your case. Just remember to use theCtrl+Ckeyboard hotkeywhen you’re ready to break out ofStart-Sleep.
These are all the ways you can stop PowerShell from closing after running a script. And one of these should come in handy when facing the problem!
Before you leave, find out how toremove preinstalled apps via PowerShell, and free up the disk space.
Let us know which one worked for you in the comments section below.
More about the topics:PowerShell
Vlad Turiceanu
Windows Editor
Passionate about technology,Windows, and everything that has a power button, he spent most of his time developing new skills and learning more about the tech world.
Coming from a solid background in PC building and software development, with a complete expertise in touch-based devices, he is constantly keeping an eye out for the latest and greatest!
User forum
0 messages
Sort by:LatestOldestMost Votes
Comment*
Name*
Email*
Commenting as.Not you?
Save information for future comments
Comment
Δ
Vlad Turiceanu
Windows Editor
Coming from a solid background in PC building and software development, he’s a Windows 11 Privacy & Security expert.