PowerTip: Use PowerShell to Wait for a Key Press

PowerTip: Use PowerShell to Wait for a Key Press

  • Comments 7
  • Likes

Summary: Learn about the Windows PowerShell version of “Pause.”

Hey, Scripting Guy! Question How can I use Windows PowerShell to wait for a key in scripts like I used to use “Pause” in the command console?

Hey, Scripting Guy! Answer Use the $Host variable in the console:

$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
$HOST.UI.RawUI.Flushinputbuffer()

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • 3 versions already and an upcoming 4th. Put the "Pause" just like it was on command console already :)

  • @Dalmiro -

    It is still there if you need it:

    PS >cmd /c pause

    Works just like it always did.

  • Hey proxb and Sean,

    if ($PSVersionTable.PSVersion.Major -ge 3) {Get-Command Pause}

  • In PowerShell v3, Pause is just a simple function.  Even if you're running PowerShell v2, you can add it to a profile or script:

    function Pause

    {

       Read-Host 'Press Enter to continue...' | Out-Null

    }

  • I like what david gave, simple and easy to use.. read-host press enter key to continue.. infact I have been using it for long time..

  • I use this... is your way better?

    If (!($psISE)){"Press any key to continue...";[void][System.Console]::ReadKey($true)}

    And, is there anything wrong with mine? (no flush buffer)

  • The above solution does not seem to work for Powershell ISE, confirmed per the following link: http://connect.microsoft.com/PowerShell/feedback/details/713218/host-ui-rawui-readkey-not-implemented-in-version-3-ctp2

    In addition, someone wrote their function to pause scripts:

    http://adamstech.wordpress.com/2011/05/12/how-to-properly-pause-a-powershell-script/

    This is similar, but much more verbose than chameleon777.

    Hope this helps someone!