* Skip to primary navigation 
  * Skip to content 
  * Skip to footer 

[ Daniel Schroeder's Programming Blog  Sharing my tips, tricks, and code to
help other developers be more productive.  ](/)

  * [ Home ](/)
  * [ Posts ](/posts/)
  * [ Categories ](/categories/)
  * [ About ](/about/)

Toggle search  __ Toggle menu

[ ![Daniel Schroeder](/assets/Site/bio-photo.jpg)
](https://blog.danskingdom.com/)

###  [ Daniel Schroeder ](https://blog.danskingdom.com/)

aka deadlydog. Developer and family man. Loves C#, .NET, PowerShell,
AutoHotkey. Automate Everything!

Follow

  * __ Regina, SK, Canada 
  * [ __ Twitter  ](https://twitter.com/deadlydog)
  * [ __ Mastodon  ](https://hachyderm.io/@deadlydog)
  * [ __ YouTube  ](https://www.youtube.com/deadlydog)
  * [ __ GitHub  ](https://github.com/deadlydog)
  * [ __ LinkedIn  ](https://www.linkedin.com/in/1danielschroeder/)
  * [ __ Stack Overflow  ](https://stackoverflow.com/users/602585/deadlydog)
  * [ __ Email  ](mailto:deadlydog@hotmail.com)
  * [ __ Blog RSS Feed  ](https://blog.danskingdom.com/feed.xml)

#  [ Keep PowerShell Console Window Open After Script Finishes Running
](https://blog.danskingdom.com/keep-powershell-console-window-open-after-
script-finishes-running/)

__ July 7, 2014  __ 2 minute read

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 ](https://blog.danskingdom.com/fix-problem-where-
windows-powershell-cannot-run-script-whose-path-contains-spaces/) , 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 ](https://blog.danskingdom.com/fix-
problem-where-windows-powershell-cannot-run-script-whose-path-contains-
spaces/) , 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