Powershell Press Any Key To Continue
Everyone once in a while I come across a PowerShell script that behaves like an old-fashioned batch file. Not that there’s anything wrong with that, but often these types of scripts put in a pause at the end of script so you can see that it finished. You might have seen a command like this in a PowerShell script.
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.' Basically I want my loop to continue until I press q but it isn't working. The rest of the loop works fine, but this stops me until I press a key and just ends after one process. Thanks so much in advance! “Press Any Key to Continue” Hello, A basic, but usefull command for powershell “Press Any Key to Continue”. 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.
Sure, it works but is hardly elegant PowerShell. First, I’m not sure you even need it. If you want to draw attention to the fact that your script ended, consider using Write-Host with a foreground (or background color).
But if you truly need the user to acknowledge that the script has finished you can use Read-Host.
I’m piping the command to Out-Null because I don’t care what the user enters, only that they hit a key.
If you prefer a move visual cue, you can always resort to the Wscript.Shell COM object.
The popup will remain until the user clicks OK. Or you can change the second parameter from 0 to another value to have the popup automatically dismiss after the specified number of seconds.
So if you need a refreshing pause, there are plenty of PowerShell options for you to use.
UPDATE:
After someone posted a comment I realized there is yet another way to do this. Add these two lines to the end of your PowerShell script.
This is very similar behavior to Read-Host but with a bit more flexibility.
Synopsis
Waits (pauses the session) until a user presses a key. For use in scripts.Syntax
- Wait-KeyPress [-Message] [-Quiet]
Description
The Wait-KeyPress cmdlet pauses the Powershell session until the user presses a key.Parameters
- Message The message to display to the user. By default the message is 'Press any key to continue...'
- Quiet Do not display a message.
Inputs
Wait-KeyPress does not accept input objects.Outputs
Wait-KeyPress does not output any Powershell objects.Notes
The are no notes associated with the Wait-KeyPress cmdlet.Example 1
This command will pause the current Powershell session with the message 'Press any key to continue...':C:PS>Wait-KeyPress
Example 2
This command will pause the current Powershell session with the message 'Hit a button to proceed...':C:PS>Wait-KeyPress -Message 'Hit a button to proceed...'
Example 3
This command will will pause the current Powershell session without displaying a message to the user:Powershell Bypass Press Any Key To Continue
C:PS>Wait-KeyPress -Quiet