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

2. Use the 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

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

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

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:

Run this Powershell script to remove default apps from Windows 10 Image Windows PowerShell has stopped working: Try these 4 fixes PowerShell is not recognized? Check out these solutions

SPONSORED Name * Email * Commenting as . Not you? Save information for future comments
Comment

Δ