{"id":2786,"date":"2013-10-01T00:01:00","date_gmt":"2013-10-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/10\/01\/rebuild-the-pause-command-with-powershell\/"},"modified":"2013-10-01T00:01:00","modified_gmt":"2013-10-01T00:01:00","slug":"rebuild-the-pause-command-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/rebuild-the-pause-command-with-powershell\/","title":{"rendered":"Rebuild the Pause Command with PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use a Windows PowerShell function to truly mimic legacy commands.\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy!\nWhat happened to &ldquo;pause&rdquo; in Windows PowerShell? Am I simply not recognizing it?\n&mdash;KL\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello KL,\nHonorary Scripting Guy, Sean Kearney, here. I&rsquo;m again filling in for our good friend, Ed. He stepped away from his keyboard for a few minutes, so I&rsquo;m quickly typing&hellip;shhhhhh!\nLet&rsquo;s &ldquo;pause&rdquo; for a moment&#8230;\nIn the old console world, we had a little friend called <strong>Pause<\/strong>. We used to do this when we wanted to wait for a key press to continue a script:<\/p>\n<p style=\"padding-left: 30px\">PAUSE\nAnd then we got a response:<\/p>\n<p style=\"padding-left: 30px\">Press any key to continue&hellip;\nAfterwards there was a massive panic as keyboards across the planet were replaced due to a severe lack of &ldquo;Any Key&rdquo; buttons. The world entered a panic. Label makers and stickers with the words &ldquo;Any Key&rdquo; were produced to sooth the population. Anarchy reigned supreme!\nWell, no it didn&rsquo;t. But we used to have a command called <strong>Pause<\/strong>. We don&rsquo;t seem to have it in Windows PowerShell&hellip;at least, it is not obvious.\nThere have been many great people before me that showed how to introduce a <strong>Pause<\/strong> in a Windows PowerShell script. There is a simple point where you tap some key on the keyboard to continue with operations.\nWe had a PowerTip a few weeks ago about how to do this:<\/p>\n<p style=\"padding-left: 30px\">$HOST.UI.RawUI.ReadKey(&#8220;NoEcho,IncludeKeyDown&#8221;) | OUT-NULL<\/p>\n<p style=\"padding-left: 30px\">$HOST.UI.RawUI.Flushinputbuffer()\nWith that, a good friend popped in with an alternate:<\/p>\n<p style=\"padding-left: 30px\">Cmd \/c pause\nNow I know that you all just love to type, type, type&mdash;it&rsquo;s indescribably delightful. But I like to function afterwards&mdash;and sometimes beforewards. Oh, right. We could make a function and add it to our profile, maybe call it <strong>Invoke-Pause<\/strong>:<\/p>\n<p style=\"padding-left: 30px\">Function INVOKE-PAUSE() {<\/p>\n<p style=\"padding-left: 30px\">$HOST.UI.RawUI.ReadKey(&#8220;NoEcho,IncludeKeyDown&#8221;) | OUT-NULL<\/p>\n<p style=\"padding-left: 30px\">$HOST.UI.RawUI.Flushinputbuffer()<\/p>\n<p style=\"padding-left: 30px\">}\nCool! Now I can pause whenever I want to by adding this to my script:<\/p>\n<p style=\"padding-left: 30px\">INVOKE-PAUSE\nAlmost like the old ways.\nBut what happens if I don&rsquo;t tell the user? Confusion! Upset people! And calls to the Help Desk!\nWe wouldn&#8217;t want that, would we?\nSo now we&rsquo;ll expand and add some features. Let&rsquo;s put in a little something that says, &ldquo;If we say nothing, display a little message.&rdquo; So we&rsquo;ll add some simple parameters to our function. Let&rsquo;s have a simple message that will default to our good old friend from CMD days:<\/p>\n<p style=\"padding-left: 30px\">$Content=&rdquo;Press any key to continue . . .&rdquo;\nAnd then something to determine whether we should display a message:<\/p>\n<p style=\"padding-left: 30px\">$DisplayMessage=$TRUE\nSo with these additions, our function will look like this:<\/p>\n<p style=\"padding-left: 30px\">Function INVOKE-PAUSE() {<\/p>\n<p style=\"padding-left: 30px\">Param(<\/p>\n<p style=\"padding-left: 30px\">$DisplayMessage=$TRUE,<\/p>\n<p style=\"padding-left: 30px\">$Content=&rdquo;Press any key to continue . . .&rdquo;<\/p>\n<p style=\"padding-left: 30px\">)<\/p>\n<p style=\"padding-left: 30px\">If ($DisplayMessage) { WRITE-HOST $Message }<\/p>\n<p style=\"padding-left: 30px\">$HOST.UI.RawUI.ReadKey(&#8220;NoEcho,IncludeKeyDown&#8221;) | OUT-NULL<\/p>\n<p style=\"padding-left: 30px\">$HOST.UI.RawUI.Flushinputbuffer()<\/p>\n<p style=\"padding-left: 30px\">}\nSo now, after executing the function, we can do this:<\/p>\n<p style=\"padding-left: 30px\">SET-PAUSE\nWhich will respond with the classic phrase &ldquo;Press any key to continue . . .&rdquo;\nBut we can now do this:<\/p>\n<p style=\"padding-left: 30px\">SET-PAUSE &ndash;DisplayMessage $FALSE\nOr we can do this:<\/p>\n<p style=\"padding-left: 30px\">SET-PAUSE &ndash;Content &ldquo;Smack the keyboard quickly to continue with what we&rsquo;re doing&rdquo;\nOf course, after you have this function defined, you add a friendly alias that makes the world more familiar:<\/p>\n<p style=\"padding-left: 30px\">NEW-Alias Pause Invoke-pause\nNow things seem a little more familiar when you try to &ldquo;pause&rdquo; for a moment.<\/p>\n<p style=\"padding-left: 30px\">Pause\nSo there you have it, KL. With a little bit of effort we can rebuild almost anything in Windows PowerShell. Now wait for it&hellip;<\/p>\n<p style=\"padding-left: 30px\">PAUSE &ndash;Content &ldquo;Await Applause&rdquo; &ndash;DisplayMessage $TRUE\nI invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Sean Kearney,<br \/><\/strong><strong>Honorary Scripting Guy and&nbsp;<\/strong><strong>Windows PowerShell MVP<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use a Windows PowerShell function to truly mimic legacy commands. &nbsp;Hey, Scripting Guy! What happened to &ldquo;pause&rdquo; in Windows PowerShell? Am I simply not recognizing it? &mdash;KL &nbsp;Hello KL, Honorary Scripting Guy, Sean Kearney, here. I&rsquo;m again filling in for our good friend, Ed. He stepped away from his keyboard for a few minutes, [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,154,61,45],"class_list":["post-2786","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-sean-kearney","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use a Windows PowerShell function to truly mimic legacy commands. &nbsp;Hey, Scripting Guy! What happened to &ldquo;pause&rdquo; in Windows PowerShell? Am I simply not recognizing it? &mdash;KL &nbsp;Hello KL, Honorary Scripting Guy, Sean Kearney, here. I&rsquo;m again filling in for our good friend, Ed. He stepped away from his keyboard for a few minutes, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=2786"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2786\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=2786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}