How to keep the shell window open after running a PowerShell script?
Errr... I should have known:
powershell -noexit <path\script>
and that's all there's to it :)
You basically have 3 options to prevent the PowerShell Console window from closing, that I describe in more detail on my blog post.
- 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"
- Per-script Fix: Add a prompt for input to the end of your script file. e.g.
Read-Host -Prompt "Press Enter to exit"
- 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.