PowerShell Closes After Running Script: How to Stop it
Discover expert resolutions that worked for others!
3 min. read
Updated on
Read our disclosure page to find out how can you help Windows Report sustain the editorial team. Read more
Key notes
- When PowerShell closes immediately after running a script, it could be due to a bug in the OS or even misconfigured settings.
- While you could spend time eliminating the underlying cause, there are a few quick tricks that help.
- Initiating a remote connection or using the Pause command is generally preferred by users.
PowerShell is 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
- Create a file called execute.ps1 on the desktop.
- 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'}
} - Once done, run the file.
- The first time you do that, press 1 to open it up again with the No exit switch.
- The second time, press 2 to initiate the remote connection to the remote computer.
2. Use the Pause command
Why not try the Pause command 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 the Enter key.
4. Execute a PowerShell script locally to run on a remote computer
You may also try to create an open.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 stop PowerShell from closing after running a script, another effective yet underrated PowerShell cmdlet:Start-Sleep -s 15
For those who donโt know, this Start-Sleep cmdlet 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 the Ctrl + C keyboard hotkey when youโre ready to break out of Start-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 to remove preinstalled apps via PowerShell, and free up the disk space.
Let us know which one worked for you in the comments section below.
User forum
0 messages