How do you do a ‘Pause’ with PowerShell 2.0?

Viewed 241180

OK, I'm losing it. PowerShell is annoying me. I'd like a pause dialog to appear, and it won't.

PS W:\>>> $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented."
At line:1 char:23
+ $host.UI.RawUI.ReadKey <<<< ("NoEcho")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
9 Answers

10 years later i came here.... Don't know since when, but "pause" works in PowerShell like it did in DOS-commandline.

You may want to use FlushInputBuffer to discard any characters mistakenly typed into the console, especially for long running operations, before using ReadKey:

Write-Host -NoNewLine 'Press any key to continue...'
$Host.UI.RawUI.FlushInputBuffer()
$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') | Out-Null

If you want to add a timer to the press any key, this works in PS 5.1:

 $i=1
 while (
                 !([Console]::KeyAvailable) -and 
                 ($i -le 10)
            ) {
           sleep 1
           Write-Host “$i..” -NoNewLine
           $i++
 }

Note that the KeyAvailable method for the c# $Host.UI.RawUI is currently bugged in a number of PS versions after PS 2 or 3.

One-liner for PS-ISE in option <5> in Michael Sorens' answer:

[Void][Windows.Forms.MessageBox]::Show("Click OK to continue.", "Script Paused")
Related