Home > PowerShell > Keep PowerShell Console Window Open After Script Finishes Running

Keep PowerShell Console Window Open After Script Finishes Running

I originally included this as a small bonus section at the end of my other post about fixing the issue of not being able to run a PowerShell script whose path contains a space, but thought this deserved its own dedicated post.

When running a script by double-clicking it, or by right-clicking it and choosing Run With PowerShell or Open With Windows PowerShell, if the script completes very quickly the user will see the PowerShell console appear very briefly and then disappear.  If the script gives output that the user wants to see, or if it throws an error, the user won’t have time to read the text.  We have 3 solutions to fix this so that the PowerShell console stays open after the script has finished running:

1. One-time solution

Open a PowerShell console and manually run the script from the command line. I show how to do this a bit in this post, as the PowerShell syntax to run a script from the command-line is not straight-forward if you’ve never done it before.

The other way is to launch the PowerShell process from the Run box (Windows Key + R) or command prompt using the -NoExit switch and passing in the path to the PowerShell file.
For example: PowerShell -NoExit “C:\SomeFolder\MyPowerShellScript.ps1”

2. Per-script solution

Add a line like this to the end of your script:

Read-Host -Prompt “Press Enter to exit”

I typically use this following bit of code instead so that it only prompts for input when running from the PowerShell Console, and not from the PS ISE or other PS script editors (as they typically have a persistent console window integrated into the IDE).  Use whatever you prefer.

# If running in the console, wait for input before closing.
if ($Host.Name -eq "ConsoleHost")
	Write-Host "Press any key to continue..."
	$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null

I typically use this approach for scripts that other people might end up running; if it’s a script that only I will ever be running, I rely on the global solution below.

3. Global solution

Adjust the registry keys used to run a PowerShell script to include the –NoExit switch to prevent the console window from closing.  Here are the two registry keys we will target, along with their default value, and the value we want them to have:

Registry Key: HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command
Description: Key used when you right-click a .ps1 file and choose Open With -> Windows PowerShell.
Default Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "%1"
Desired Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "& \"%1\""

Registry Key: HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command
Description: Key used when you right-click a .ps1 file and choose Run with PowerShell (shows up depending on which Windows OS and Updates you have installed).
Default Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"
Desired Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoExit "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & \"%1\""

The Desired Values add the –NoExit switch, as well wrap the %1 in double quotes to allow the script to still run even if it’s path contains spaces.

If you want to open the registry and manually make the change you can, or here is the registry script that we can run to make the change automatically for us:

Windows Registry Editor Version 5.00

@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit \"& \\\"%1\\\"\""

@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit \"-Command\" \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & \\\"%1\\\"\""

You can copy and paste the text into a file with a .reg extension, or just

Simply double-click the .reg file and click OK on the prompt to have the registry keys updated.  Now by default when you run a PowerShell script from File Explorer (i.e. Windows Explorer), the console window will stay open even after the script is finished executing.  From there you can just type exit and hit enter to close the window, or use the mouse to click the window’s X in the top right corner.

If I have missed other common registry keys or any other information, please leave a comment to let me know.  I hope you find this useful.

Happy coding!

  1. Ross K
    August 26th, 2015 at 13:16 | #1

    Thank you for writing this, I found the Global Solutions to be very helpful. However, I have encountered two issues:

    1) the first regedit path does not exist: I can navigate to HKEY_CLASSES_ROOT\Applications\ but it does not contain powershell.exe. Has the directory been moved recently? I have powershell 3.0

    2) I am running powershell from a c# executable using the System.Diagnostics.ProcessStartInfo class. I can run any script that I provide to the ProcessStartInfo.Arguments property, but powershell immediately closes after the script completes. What do I need to modify to prevent powershell from closing after script execution when powershell is launched from a c# application?

  2. August 27th, 2015 at 09:51 | #2

    @Ross K
    For #1 it will probably depend on your OS version. Hopefully one of the 2 entries is in your registry. If not, the .reg file will add it I believe and may still work. Otherwise you may just have to do a Find in your registry for “powershell.exe” and find the equivalent key.

    For #2 you should just need to include “-NoExit” in the ProcessStartInfo.Arguments. Also, you may need to toggle the UseShellExecute property as well; I can never remember if we want it to be true or false.

  3. evald
    September 28th, 2015 at 09:42 | #3

    for #2 it does not work your approach.

  4. September 28th, 2015 at 12:02 | #4

    I’m assuming that you are either just doing something wrong (a typo perhaps), or that you are expecting this to pause when running the script from a PowerShell IDE, such as PowerShell ISE or PowerGui. #2 will only cause the script to pause if it’s being executed from the native PowerShell command prompt.

  1. No trackbacks yet.