92

I have a very short PowerShell script that connects to a server and imports the AD module. I'd like to run the script simply by double clicking, but I'm afraid the window immediately closes after the last line.

How can I sort this out?

1

6 Answers 6

157

You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post.

  1. One-time Fix: Run your script from the PowerShell Console, or launch the PowerShell process using the -NoExit switch. e.g. PowerShell -NoExit "C:\SomeFolder\SomeScript.ps1"
  2. Per-script Fix: Add a prompt for input to the end of your script file. e.g. Read-Host -Prompt "Press Enter to exit"
  3. Global Fix: Change your registry key by adding the -NoExit switch to always leave the PowerShell Console window open after the script finishes running.
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\""

See my blog for more information and a script to download that will make the registry change for you.

5
  • 4
    OPs answer also works as per-script fix: wrap your script with a powershell -NoExit -Command { <The script to actually run> } Enables user to start the script via UI and see the results without editing registry keys Jan 9, 2019 at 9:45
  • How can I make a shortcut that opens a PowerShell window (not cmd) with -NoExit and runs SomeScript.ps1?
    – xr280xr
    Jun 20, 2022 at 17:00
  • It looks like start is what I needed. Created a batch file with start PowerShell -NoExit -File "[.ps1 script path]". Without start it was running PowerShell within the Windows Command prompt.
    – xr280xr
    Jun 20, 2022 at 17:03
  • #3 doesn't work on Win 10 Pro v 10.0.19044 Build 19044 Jun 29, 2022 at 13:43
  • You could also associate the PowerShell ISE application with your script.
    – Rich M
    Jan 26 at 13:35
30

Errr... I should have known:

powershell -noexit <path\script> 

and that's all there's to it :)

1
  • 5
    @IgorGanapolsky The problem might be that you are appending -NoExit after the script path, it needs to go before. I think if it is after it passes it as an argument to the script instead of powershell.exe. Oct 22, 2015 at 18:00
6

The solution below prevents the script from closing when running Powershell ISE and allows the script to close otherwise.

# 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
}
2

Just add pause on a new line at the bottom of the script, as in a batch file.

1
  • This does not work for me. When running the script with the 'Pause' line at the end, PowerShell launches and I'm presented with "Press Enter to continue...:" When I press Enter, the script runs and then closes on completion as it would without the 'Pause' function.
    – Rich M
    Jan 26 at 13:45
2

You can add pause at the end of your script if you just want the window to stay open, or you can add powershell if you want to be able to run commands afterwards (obviously don't do the second option if anyone else will use your code).

1

In my own case, I wanted to add powershell to context menu on windows 7. That is right clicking on a folder or inside a folder to get a menu to launch Powershell window without it closing after launch. The answers here helped me do just that and I want to share it here incase it helps someone else.

  • Launch registry editor by pressing WIN + R type regedit.exe and hit enter
  • Navigate to HKEY_CLASSES_ROOT\Directory\Background\shell
  • Right click on shell and create a key give it a name e.g PowershellHere
  • On the right pane, double click on Default and provide a descriptive name e.g Powershell Here
  • Right click on the PowershellHere key you created earlier and create a new key and name it "command" please make sure you name it exactly so but without the quotes.
  • On the right pane, double click on Default and then type the command below
  • C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit -Command CD '"%1"' -noexit flag makes sure that the Powershell windows does not close again immediately after launch '"%1"' flag represents the folder you right clicked -Command CD '"%1"' will ensure the Powershell changes into the right clicked directory.

To make the right click work inside a folder meaning right clicking an empty space inside a folder, repeat the steps but this time, the registry location is:

HKEY_CLASSES_ROOT\Directory\shell
And the command is:
C:\Windows\system32\WindowsPowerShell\v1.0\PowerShell.exe -noexit

Tested on windows 7 ultimate sp1 but I think I might work for later versions of Windows as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.