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.

  • Download DriverFix (verified download file).

  • Click Start Scan to find all problematic drivers.

  • Click Update Drivers to get new versions and avoid system malfunctionings.

  • DriverFix has been downloaded by 0 readers this month.

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

  • You need to 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’}
  • }
  • Run it. 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 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

If the advices above haven’t solved your issue, your PC may experience deeper Windows problems. We recommend downloading this PC Repair tool (rated Great on TrustPilot.com) to easily address them. After installation, simply click the Start Scan button and then press on Repair All.

Still having issues? Fix them with this tool:

SPONSORED

  • PowerShell

Email *

Commenting as . Not you?

Comment