Quick Tip – How to Wait for User Keypress in #PowerShell

You’ve developed a PowerShell script that returns some useful information to the user.  At the end of the script execution, you want the user to “Press any key to continue…” before exiting.  How to do it?

Solution 1: For PowerShell Console

Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

If you run the script above in Windows PowerShell commandline console, you will get the following results when you press the Enter key:

VirtualKeyCode  Character  ControlKeyState  KeyDown
--------------  ---------  ---------------  -------
            13  ...        0                True

However, if you are running your script in PowerShell ISE, you will receive the following error:

Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented."

To resolve this error in PowerShell ISE, see the next solution.

Solution 2: Works in PowerShell ISE

Here is a simple way to pause the script execution and wait for the user to press the ENTER key to continue. This works for both the PowerShell commandline console as well as in the PowerShell ISE.

read-host “Press ENTER to continue...”

Solution 3: MessageBox UI

Another way to pause the script execution and wait for the user’s interaction is by showing a MessageBox UI. This works for both the PowerShell commandline console as well as in the PowerShell ISE.

$Shell = New-Object -ComObject "WScript.Shell"
$Button = $Shell.Popup("Click OK to continue.", 0, "Hello", 0)

This will result in a MessageBox UI as follows:

messagebox

Solution 4: Pause function

Here is an encompassing solution that works whether you are running your script in the PowerShell commandline console or in the PowerShell ISE:

Function Pause ($Message = "Press any key to continue...") {
   # Check if running in PowerShell ISE
   If ($psISE) {
      # "ReadKey" not supported in PowerShell ISE.
      # Show MessageBox UI
      $Shell = New-Object -ComObject "WScript.Shell"
      $Button = $Shell.Popup("Click OK to continue.", 0, "Hello", 0)
      Return
   }

   $Ignore =
      16,  # Shift (left or right)
      17,  # Ctrl (left or right)
      18,  # Alt (left or right)
      20,  # Caps lock
      91,  # Windows key (left)
      92,  # Windows key (right)
      93,  # Menu key
      144, # Num lock
      145, # Scroll lock
      166, # Back
      167, # Forward
      168, # Refresh
      169, # Stop
      170, # Search
      171, # Favorites
      172, # Start/Home
      173, # Mute
      174, # Volume Down
      175, # Volume Up
      176, # Next Track
      177, # Previous Track
      178, # Stop Media
      179, # Play
      180, # Mail
      181, # Select Media
      182, # Application 1
      183  # Application 2

   Write-Host -NoNewline $Message
   While ($KeyInfo.VirtualKeyCode -Eq $Null -Or $Ignore -Contains $KeyInfo.VirtualKeyCode) {
      $KeyInfo = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
   }
}

You will see Press any key to continue... if you are running in the PowerShell commandline console, or a MessageBox UI if you are running in PowerShell ISE.

Have fun with PowerShell!

Leave a Comment