Press Any Key to Continue in PowerShell

Rohan Timalsina Jan 30, 2023 Jan 09, 2022
  1. Use ReadKey (System.Console) to Enable the press any key to continue in the PowerShell
  2. Use ReadKey (Console) to Enable the press any key to continue in the PowerShell
  3. Use ReadKey (RawUI) to Enable the press any key to continue in the PowerShell
  4. Use Read-Host to Enable the press any key to continue in the PowerShell
  5. Use cmd /c 'pause' Command to Enable the press any key to continue in the PowerShell
  6. Use timeout to Enable the press any key to continue in the PowerShell
Press Any Key to Continue in PowerShell

This tutorial will teach you to pause execution in PowerShell.

It allows you to enable the press any key to continue dialog in the PowerShell. It is generally used to wait for user input or another process.

It also helps to slow down or pause the execution in the PowerShell. It mostly does not work with the keys like Ctrl, Shift, Windows, and Alt.

Use ReadKey (System.Console) to Enable the press any key to continue in the PowerShell

The System.Console ReadKey can be executed as shown below. It accepts any key except Shift, Alt, Ctrl, and other modifier keys.

[void][System.Console]::ReadKey($true)

Use ReadKey (Console) to Enable the press any key to continue in the PowerShell

You can use the [Console]::ReadKey() to enable the press any key to continue in the PowerShell. It can read keys and modifiers while pausing the execution. It excludes Shift, Alt, Ctrl, and other modifier keys.

[Console]::ReadKey()

When the key is pressed, it shows the KeyChar, Key, and Modifiers values. This data is stored as a System.ConsoleKeyInfo object.

Output:

KeyChar Key Modifiers
------- --- ---------
      a   A         0

Use ReadKey (RawUI) to Enable the press any key to continue in the PowerShell

This method is similar to the [Console]::ReadKey(). It accepts any key, including Ctrl, Shift, ? Win, Alt, and other modifier keys.

There are different ReadKeyOptions that can be passed in RawUI ReadKey method, such as IncludeKeyDown, IncludeKeyUp, NoEcho, and AllowCtrlC.

$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

After we press the q, it will show the following result.

Output:

VirtualKeyCode Character ControlKeyState KeyDown
-------------- --------- --------------- -------
            81         q       NumLockOn    True

Use Read-Host to Enable the press any key to continue in the PowerShell

Read-Host is the most common method to prompt user input. You can use this method to pause the execution when prompting user input in the PowerShell.

Read-Host -Prompt "Press any key to continue"

After pressing the key, you need to press Enter to exit the pause mode. It displays the entered text in the output.

Output:

Press any key to continue: Hello
Hello

Use cmd /c 'pause' Command to Enable the press any key to continue in the PowerShell

The cmd /c pause command displays the Press any key to continue . . . and pauses the execution until a key is pressed.

cmd /c pause

Output:

Press any key to continue . . .

Use timeout to Enable the press any key to continue in the PowerShell

The timeout command can pause the execution for a specific period of time or infinite time. You can use the /t option to specify the time in seconds. The valid value of the specified timeout ranges from -1 to 99999.

The command below waits for the 5 seconds if a key is not pressed.

timeout /t 5

Output:

Waiting for 5 seconds, press a key to continue ...

The -1 value is used to pause the execution for an infinite amount of time if a key is not pressed.

timeout /t -1

Output:

Press any key to continue ...
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell Input