124

According to Microsoft's documentation, read-host lets the user type some input, and then press enter to continue. Not exactly the correct behavior if you want to have "Press any key to continue". (Wait... where's the Any key?!)

Is there a way to accomplish this? Something like read-char?

I've tried searching for "single character input" and "powershell input" to see if I could find a list of all ways to get input without much luck. And several Stack Overflow questions that looked hopeful use read-host which doesn't actually work for "press any key..." functionality.

Jeff B
  • 8,572
  • 17
  • 61
  • 140
  • 5
    Nice old [powershell tip of the week](http://technet.microsoft.com/en-us/library/ff730938.aspx) (found by searching for Powershell and Pause, pause being the command in command prompt/MS dos that showed that text) – Damien_The_Unbeliever Jan 02 '14 at 15:36

3 Answers3

197

Here is what I use.

Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
Jeff B
  • 8,572
  • 17
  • 61
  • 140
Knuckle-Dragger
  • 6,644
  • 4
  • 26
  • 41
  • 21
    I get an error: ```Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented." At C:\file.ps1:26 char:5 + $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : NotImplementedException``` – gakera Oct 23 '15 at 11:18
  • 22
    @gakera This does not work under Powershell ISE. – Artemix Feb 15 '16 at 15:41
  • 1
    Please check out the answer from (Jerry G) for how to fix on ISE. These problems are what led me to this question – ZaxLofful Aug 15 '18 at 17:30
  • It works properly when running the script from the PowerShell command. – Kevy Granero Nov 12 '21 at 06:53
61

I've created a little Powershell function to emulate MSDOS pause. This handles whether running Powershell ISE or non ISE. (ReadKey does not work in powershell ISE). When running Powershell ISE, this function opens a Windows MessageBox. This can sometimes be confusing, because the MessageBox does not always come to the forefront. Anyway, here it goes:

Usage: pause "Press any key to continue"

Function definition:

Function pause ($message)
{
    # Check if running Powershell ISE
    if ($psISE)
    {
        Add-Type -AssemblyName System.Windows.Forms
        [System.Windows.Forms.MessageBox]::Show("$message")
    }
    else
    {
        Write-Host "$message" -ForegroundColor Yellow
        $x = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
}
Cullub
  • 2,901
  • 3
  • 30
  • 47
Jerry G
  • 934
  • 7
  • 8
  • 4
    Does not work in VSCode – ZaxLofful Aug 15 '18 at 17:30
  • $message ="Press any key to continue" pause ($message) for me it is working in ise and powershell , I have add just this lines , maybe you need to do the same in vs code ?! @ZaxLofful – leonidaa Feb 08 '21 at 14:24
36

Check out the ReadKey() method on the System.Console .NET class. I think that will do what you're looking for.

http://msdn.microsoft.com/en-us/library/system.console.readkey(v=vs.110).aspx

Example:

Write-Host -Object ('The key that was pressed was: {0}' -f [System.Console]::ReadKey().Key.ToString());