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, 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

[HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit \"& \\\"%1\\\"\""

[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0\Command]
@="\"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 download it here.

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!

Comments

Ross K

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?

deadlydog

@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.

deadlydog

@evald 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.

Patrick McCoy

Hi, I realize this post is old but I had a question. I wrote this .bat file to run a PowerShell script that’s saved in the same folder. I’ve tried a few different ways to make sure PowerShell stays open but so far it doesn’t work. The PowerShell script itself runs on my local PC but is remotely starting a process on a VM. Here’s what I have:

@ECHO OFF SET ThisScriptsDirectory=%~dp0 SET PowerShellScriptPath=%ThisScriptsDirectory%Stage_1st_Half.ps1 PowerShell -NoProfile -ExecutionPolicy Bypass -Command “& ‘PowerShellScriptPath’”;

I’ve tried the above with the following at the end:

if ($Host.Name -eq “ConsoleHost”) { Write-Host “Press any key to continue…” $Host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyUp”) > $null }

And also

Read-Host -Prompt “Press Enter to exit”

But each time I run it the window opens and flashes closed. I don’t have admin privileges (and won’t be able to get them) on my work PC and my goal for this is to make running these PowerShell scripts easier for my less tech-savvy teammates who need to run this process at certain times, without them calling me to help them out at 10pm on a maintenance evening. I’m definitely not an expert coder but am trying to learn more and teach myself as much as I can. Do you have any ideas why I can’t get the PowerShell window to stay open? Thank you for your assistance.

AndrewF

@evald I had the same issue when I copy-pasted the code. The quotation marks are wrong. Try retyping it yourself instead of copying copy-pasting and see if that works

CJ

@Patrick McCoy Try the following: SET ThisScriptsDirectory=%~dp0 SET PowerShellScriptPath=%ThisScriptsDirectory%\Stage_1st_Half.ps1 PowerShell -NoExit -NoProfile -ExecutionPolicy Bypass -Command “& ‘%PowerShellScriptPath%’”

You seemed to be missing the following in your code block:

  • a backslash after %ThisScriptsDirectory% (2nd line)
  • -NoExit option after PowerShell (3rd line)
  • %s around PowerShellScriptPath (3rd line)
  • single quotes need to surround %PowerShellScriptPath% - the first one looks like a backtick (`) and not a single quote (‘) (3rd line)
Colin

I don’t think any of these address the situation where you want to have a script to run an any computer authorized to run the script, and then after running, the script leaves the PowerShell window open for the user to continue entering commands at the prompt.

In the old .BAT/.CMD command prompt days, this was accomplished with a simple @cmd /k at the end of the bat file.

Is there really no equivalent with PowerShell? This seems a common need: I want to run a script that connects to a remote computer (an Exchange server in this case), and then drops me at the PowerShell prompt to work on that remote computer in PowerShell.

Commodore Vic

i had the same question as Colin, i have some users that need to ping a handful of computers that are not admins, was able to make a script and even add the Read-Host -Prompt “Press Enter to exit” and works great for me but would like to have a shortcut for the select users to simply click on. having a few issues with this final step

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...