October 1st, 2013

Rebuild the Pause Command with PowerShell

Doctor Scripto
Scripter

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 

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.