Rebuild the Pause Command with PowerShell

Rebuild the Pause Command with PowerShell

  • Comments 11
  • Likes

Summary: Use a Windows PowerShell function to truly mimic legacy commands.

Hey, Scripting Guy! Question Hey, Scripting Guy!

What happened to “pause” in Windows PowerShell? Am I simply not recognizing it?

—KL

Hey, Scripting Guy! Answer Hello KL,

Honorary Scripting Guy, Sean Kearney, here. I’m again filling in for our good friend, Ed. He stepped away from his keyboard for a few minutes, so I’m quickly typing…shhhhhh!

Let’s “pause” for a moment...

In the old console world, we had a little friend called Pause. We used to do this when we wanted to wait for a key press to continue a script:

PAUSE

And then we got a response:

Press any key to continue…

Afterwards there was a massive panic as keyboards across the planet were replaced due to a severe lack of “Any Key” buttons. The world entered a panic. Label makers and stickers with the words “Any Key” were produced to sooth the population. Anarchy reigned supreme!

Well, no it didn’t. But we used to have a command called Pause. We don’t seem to have it in Windows PowerShell…at least, it is not obvious.

There have been many great people before me that showed how to introduce a Pause in a Windows PowerShell script. There is a simple point where you tap some key on the keyboard to continue with operations.

We had a PowerTip a few weeks ago about how to do this:

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

$HOST.UI.RawUI.Flushinputbuffer()

With that, a good friend popped in with an alternate:

Cmd /c pause

Now I know that you all just love to type, type, type—it’s indescribably delightful. But I like to function afterwards—and sometimes beforewards. Oh, right. We could make a function and add it to our profile, maybe call it Invoke-Pause:

Function INVOKE-PAUSE() {

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

$HOST.UI.RawUI.Flushinputbuffer()

}

Cool! Now I can pause whenever I want to by adding this to my script:

INVOKE-PAUSE

Almost like the old ways.

But what happens if I don’t tell the user? Confusion! Upset people! And calls to the Help Desk!

We wouldn't want that, would we?

So now we’ll expand and add some features. Let’s put in a little something that says, “If we say nothing, display a little message.” So we’ll add some simple parameters to our function. Let’s have a simple message that will default to our good old friend from CMD days:

$Content=”Press any key to continue . . .”

And then something to determine whether we should display a message:

$DisplayMessage=$TRUE

So with these additions, our function will look like this:

Function INVOKE-PAUSE() {

Param(

$DisplayMessage=$TRUE,

$Content=”Press any key to continue . . .”

)

If ($DisplayMessage) { WRITE-HOST $Message }

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

$HOST.UI.RawUI.Flushinputbuffer()

}

So now, after executing the function, we can do this:

SET-PAUSE

Which will respond with the classic phrase “Press any key to continue . . .”

But we can now do this:

SET-PAUSE –DisplayMessage $FALSE

Or we can do this:

SET-PAUSE –Content “Smack the keyboard quickly to continue with what we’re doing”

Of course, after you have this function defined, you add a friendly alias that makes the world more familiar:

NEW-Alias Pause Invoke-pause

Now things seem a little more familiar when you try to “pause” for a moment.

Pause

So there you have it, KL. With a little bit of effort we can rebuild almost anything in Windows PowerShell. Now wait for it…

PAUSE –Content “Await Applause” –DisplayMessage $TRUE

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Sean Kearney,
Honorary Scripting Guy and Windows PowerShell MVP 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Are the references to SET-PAUSE correct?

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

  • @Josh

    You caught that correctly.  My brain was working WAY too fast and was out thinking my fingers.   It should all be "INVOKE-PAUSE"

    @Vern_Anderson

    You are correct! In version 3 they added a "pause" but it doesn't work the same way as the original pause.  You must deliberately hit Enter.  I wanted one that mimicked the way Dos did it completely (Right down to the words)

    Of course I can get really crazy down and have custom phrases, catch specific keys ....

    But honestly, I just wanted my old "pause" back :)

    Sean

  • Right on! Thank you Sean you're absolutely right

  • Wouldn't it be just as easy to use read-host?  i.e. Read-Host "Press ENTER to continue..."

  • @Scott

    That's exactly what the built-in Pause function does in PowerShell 3.0.  You can see its definition by typing $function:pause (and it happens to be this:  Read-Host 'Press Enter to continue...' | Out-Null  )

    Sean already mentioned that he wanted to be able to press any key, not just Enter.

  • @Scott

    Yep, correct.  Exactly what Version 3 does and you're correct that works just as simply :)

    I think I need a little more "Pause" in my life as it's obvious just how quickly my fingers were working vs. my Brain.  Not quite sure How I kept going "SET-PAUSE" when was thinking "INVOKE-PAUSE".

    Oh I know the answer..... "Sean, step back from the Espresso machine.   Move VERY slowly away from the Espresso machine..." ;)

    Sean

  • The code does not work, replace $Message with $Content as in:

    If ($DisplayMessage) { WRITE-HOST $Content }

  • Don't forget the built in commandlet

    Read-host -prompt "hit enter to continue"

  • Doesn't work and gives the following error. Exception calling "ReadKey" with "1" argument(s): "The method or operation is not implemented." At C:\scripting\PS\test.ps1:3 char:1 + $HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : NotImplementedException Exception calling "FlushInputBuffer" with "0" argument(s): "The method or operation is not implemented." At C:\scripting\PS\test.ps1:5 char:1 + $HOST.UI.RawUI.Flushinputbuffer() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : NotImplementedException

  • $HOST.UI.RawUI.ReadKey - Works in the PowerShell console host but is not expected to work in the ISE. (As per Microsoft)