In the Newsgroup Microsoft.Public.Windows.PowerShell, BJ Stigall asked what the equivalent of the batch file PAUSE function was in PowerShell.

There were lots of good answers that came close but I think the one below comes the closest (and a little bit better).

function Pause ($Message=”Press any key to continue…”)
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)
Write-Host “”

Here is an example of it in use:

PS> pause
Press any key to continue…
PS> pause “Press any key to win a million dollars…”
Press any key to win a million dollars…

Of course you can see me pressing a key but go ahead and use this function and see for yourself. The key was to realize that the ReadKey() function takes Options. How do you do that? Let me show you a trick:

PS> $host.UI.RawUI.ReadKey

MemberType : Method
OverloadDefinitions : {System.Management.Automation.Host.KeyInfo ReadKey(Re
adKeyOptions options),
ost.KeyInfo ReadKey()}
TypeNameOfValue : System.Management.Automation.PSMethod
Value : System.Management.Automation.Host.KeyInfo ReadKey(Rea
dKeyOptions options), System.Management.Automation.Ho
st.KeyInfo ReadKey()
Name : ReadKey
IsInstance : True

What this is showing you is that in PowerShell, if you just provide the name of a method but don’t call it by adding the (), we return the information about the method. This shows you that there is an overload that takes a parameter Options. From there I guessed that it would be in the same namespace as the method:

PS> [System.Management.Automation.Host.ReadKeyOptions]

IsPublic IsSerial Name BaseType
——– ——– —- ——–
True True ReadKeyOptions System.Enum

This showed that it was an enum so the question then is – what are the values of the ENUM? I always just give a bad value and let PowerShell’s error message tell me what the acceptable values are:

PS> [System.Management.Automation.Host.ReadKeyOptions]””
Cannot convert value “” to type “System.Management.Automation.Host.ReadKeyO
ptions” due to invalid enumeration values. Specify one of the following enu
meration values and try again. The possible enumeration values are “AllowCt
rlC, NoEcho, IncludeKeyDown, IncludeKeyUp”
At line:1 char:51
+ [System.Management.Automation.Host.ReadKeyOptions] <<<<

I realized that I wanted NOECHO (this was a the behavior in CMD.EXE’s PAUSE that the other answers didn’t replicate). I tried that and got a error (Notice that I just provide a STRING “NoEcho” and PowerShell does the rest for me:

PS> $host.UI.RawUI.ReadKey(“NoEcho”)
Exception calling “ReadKey” with “1” argument(s): “Cannot read key options.
To read options either IncludeKeyDown,IncludeKeyUp or both must be set.”
At line:1 char:23
+ $host.UI.RawUI.ReadKey <<<< (“NoEcho”)

The error message is telling me that I must also do a bit OR with IncludeKeyDown or IncludeKeyUp so that only other trick was to realize that you could use a “,” in the string and PowerShell does that for you.

PS> $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)

VirtualKeyCode Character ControlKeyState KeyDown
————– ——— ————— ——-
65 a 0 True


Of course this then tells you what the user typed which we don’t want so we just assign this to $NULL and Bobs’ your uncle!

PS> $null = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)


BTW – if you are reading the blog after the weekend, please don’t miss this entry Supporting –Whatif, -Confirm, -Verbose in Scripts – it is super important.

Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at:
Visit the Windows PowerShell ScriptCenter at: