- Powershell Wait For Key
- Powershell Press Any Key To Continue
- Powershell Press Any Key To Continue Timeout
But Read-Host 'Press any key to exit.' Allows the user to type anything before pressing enter to exit. So it really should be Read-Host 'Press the enter key to exit.' That said, what is the command to exit when 'any key' is pressed? – Clay Jul 26 '18 at 17:46. Hopefully this is an easy question but I'm stumped. Got a 2k3 server with MS SMTP service, acting as a limited mail relay. When I telnet to port 25 on the server from the server, the SMTP service. From time to time it is nice to have a “Press any key to continue.” break point in a script to allow the user to review the status of an operation or just to add a user interaction to acknowledge the completion of an operation. When the pause command is run, PowerShell will display the message ' Press Enter to continue. ' and then halt any further execution until the user presses the ENTER key on the keyboard. In Windows CMD the PAUSE command displays the message 'Press any key to continue.'
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.
Powershell Wait For Key
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:SomeFolderMyPowerShellScript.ps1”
2. Per-script solution
Add a line like this to the end of your script:
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.
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.
Powershell Press Any Key To Continue
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:
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:
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.
Powershell Press Any Key To Continue Timeout
Happy coding!