Derrick Bell
Derrick Bell

Reputation: 2715

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

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

Upvotes: 142

Views: 248405

Answers (12)

c z
c z

Reputation: 8977

For ReadKey, you might want instead:

[Console]::TreatControlCAsInput = $True
$Key = [Console]::ReadKey($True);
[Console]::TreatControlCAsInput = $False

And then select on $Key, otherwise you annoyingly can't ctrl+c out of whatever it is you're doing.

Upvotes: 0

PlayLORD-SysOp
PlayLORD-SysOp

Reputation: 320

Same as others, EXCEPT this mimicks cmd pause exactly down the line breaks and blinking cursor position

Write-Host 'Press any key to continue . . . '-NoNewLine;[void][System.Console]::ReadKey($FALSE);Write-Host

Upvotes: 0

Camilo
Camilo

Reputation: 397

Add just one command:

pause

Here is the explanation

Upvotes: 1

rerun
rerun

Reputation: 25505

I assume that you want to read input from the console. If so, use Read-Host -Prompt "Press Enter to continue".

Upvotes: 46

loxia_01
loxia_01

Reputation: 53

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

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

Upvotes: 0

Blaisem
Blaisem

Reputation: 637

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.

Upvotes: 1

UdeF
UdeF

Reputation: 255

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

Upvotes: 3

Teraokay
Teraokay

Reputation: 118

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

Upvotes: 1

iRon
iRon

Reputation: 23663

In addition to Michael Sorens' answer:

<6> ReadKey in a new process

Start-Process PowerShell {[void][System.Console]::ReadKey($true)} -Wait -NoNewWindow
  • Advantage: Accepts any key but properly excludes Shift, Alt, Ctrl modifier keys.
  • Advantage: Works in PS-ISE.

Upvotes: 5

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

The solutions like cmd /c pause cause a new command interpreter to start and run in the background. Although acceptable in some cases, this isn't really ideal.

The solutions using Read-Host force the user to press Enter and are not really "any key".

This solution will give you a true "press any key to continue" interface and will not start a new interpreter, which will essentially mimic the original pause command.

Write-Host "Press any key to continue..."
[void][System.Console]::ReadKey($true)

Upvotes: 22

Carlos Nunez
Carlos Nunez

Reputation: 2147

cmd /c pause | out-null

(It is not the PowerShell way, but it's so much more elegant.)

Save trees. Use one-liners.

Upvotes: 98

Michael Sorens
Michael Sorens

Reputation: 36708

I think it is worthwhile to recap/summarize the choices here for clarity... then offer a new variation that I believe provides the best utility.

<1> ReadKey (System.Console)

write-host "Press any key to continue..."
[void][System.Console]::ReadKey($true)
  • Advantage: Accepts any key but properly excludes Shift, Alt, Ctrl modifier keys.
  • Disadvantage: Does not work in PS-ISE.

<2> ReadKey (RawUI)

Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  • Disadvantage: Does not work in PS-ISE.
  • Disadvantage: Does not exclude modifier keys.

<3> cmd

cmd /c Pause | Out-Null
  • Disadvantage: Does not work in PS-ISE.
  • Disadvantage: Visibly launches new shell/window on first use; not noticeable on subsequent use but still has the overhead

<4> Read-Host

Read-Host -Prompt "Press Enter to continue"
  • Advantage: Works in PS-ISE.
  • Disadvantage: Accepts only Enter key.

<5> ReadKey composite

This is a composite of <1> above with the ISE workaround/kludge extracted from the proposal on Adam's Tech Blog (courtesy of Nick from earlier comments on this page). I made two slight improvements to the latter: added Test-Path to avoid an error if you use Set-StrictMode (you do, don't you?!) and the final Write-Host to add a newline after your keystroke to put the prompt in the right place.

Function Pause ($Message = "Press any key to continue . . . ") {
    if ((Test-Path variable:psISE) -and $psISE) {
        $Shell = New-Object -ComObject "WScript.Shell"
        $Button = $Shell.Popup("Click OK to continue.", 0, "Script Paused", 0)
    }
    else {     
        Write-Host -NoNewline $Message
        [void][System.Console]::ReadKey($true)
        Write-Host
    }
}
  • Advantage: Accepts any key but properly excludes Shift, Alt, Ctrl modifier keys.
  • Advantage: Works in PS-ISE (though only with Enter or mouse click)
  • Disadvantage: Not a one-liner!

Upvotes: 285

Related Questions