{"id":17451,"date":"2010-08-12T00:01:00","date_gmt":"2010-08-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/08\/12\/pausing-a-windows-powershell-script-to-receive-keyboard-input\/"},"modified":"2010-08-12T00:01:00","modified_gmt":"2010-08-12T00:01:00","slug":"pausing-a-windows-powershell-script-to-receive-keyboard-input","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/pausing-a-windows-powershell-script-to-receive-keyboard-input\/","title":{"rendered":"Pausing a Windows PowerShell Script to Receive Keyboard Input"},"content":{"rendered":"<p>&nbsp;\n<b>Summary<\/b>: Pausing execution of a Windows PowerShell script to receive keyboard input can be as simple as using the <b>Read-Host<\/b> cmdlet. But other methods are available.\n&nbsp;\n<span class=\"Apple-style-span\" style=\"text-indent: 0px;border-collapse: separate;font: medium 'Times New Roman';letter-spacing: normal;color: #000000\"><span class=\"Apple-style-span\" style=\"font-family: Arial, Helvetica, sans-serif;font-size: 12px\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" style=\"border-width: 0px\"><\/span><\/span>Hey, Scripting Guy! I would like to pause a Windows PowerShell script and wait for user input. When the user types a particular key on the keyboard, I would like the script to continue. I found an old Hey, Scripting Guy! post called <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2004\/10\/05\/how-can-i-pause-a-script-and-then-resume-it-when-a-user-presses-a-key-on-the-keyboard.aspx\">How Can I Pause a Script and Then Resume It When a User Presses a Key on the Keyboard?<\/a> but it is written for VBScript. I do not think I want to attempt using <b>stdin<\/b> from a Windows PowerShell script. I also found an article called <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2009\/08\/21\/hey-scripting-guy-can-i-pause-and-resume-scripts-with-windows-powershell.aspx\">Can I Pause and Resume Scripts with Windows PowerShell?<\/a> that was written for the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/2009+summer+scripting+games\/\">2009 Summer Scripting Games<\/a>, but that does not work with user input. Is the absence of a solution indicative that I cannot accomplish this using Windows PowerShell?\n&#8212; TC\n&nbsp;\n<span class=\"Apple-style-span\" style=\"text-indent: 0px;border-collapse: separate;font: medium 'Times New Roman';letter-spacing: normal;color: #000000\"><span class=\"Apple-style-span\" style=\"font-family: Arial, Helvetica, sans-serif;font-size: 12px\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" style=\"border-width: 0px\"><\/span><\/span>Hello TC,\nMicrosoft Scripting Guy Ed Wilson here. I was up late last night talking to the <a href=\"http:\/\/www.sage-au.org.au\/welcome\">System Administrators Guild of Australia (SAGE-AU)<\/a> at their conference in Tasmania. It was fun, but I would rather have been there in person. Tasmania is one of the places I have not had the chance to visit. I fell in love with Tasmanian strawberries when I was in Sydney because they are super sweet, but recently they have started <a href=\"http:\/\/www.themercury.com.au\/article\/2010\/01\/20\/122851_food-wine.html\">growing strawberries that are in the shape of a heart<\/a>. Those would make a wonderful present for my significant other.\nAnyway, TC, sometimes things are so simple that they do not receive much publicity. One such example is pausing a script to wait for user input.<\/p>\n<p style=\"padding-left: 30px\"><strong>PauseScriptReadName.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #2b91af\">$a<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">read-host<\/span><span style=\"color: #808080\"> &#8220;what is your name?&#8221; <br \/>&#8220;hello $a&#8221;<\/span>\nWhen the PauseScriptReadName.ps1 script runs in the Windows PowerShell ISE, a pop-up window appears, as shown in the following image.\n<a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/august\/hey0812\/hsg-08-12-10-01.jpg\"><img decoding=\"async\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/august\/hey0812\/hsg-08-12-10-01.jpg\" border=\"0\" style=\"max-width: 550px\"><\/a>\nThere is, of course, one teeny tiny problem with the script as written. It will pause forever and ever, until the user enters data, or until the computer either restarts or shuts down. This behavior could have adverse consequences to say the least.\nTC, a completely different approach is seen in the pauseTimer.ps1 script shown here: <\/p>\n<p style=\"padding-left: 30px\"><strong>pauseTimer.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">Requires<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-version<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #2b91af\">$timer<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">10<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-IsIseHost<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Synopsis<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Determines<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">you<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">are<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">running<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Windows<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">PowerShell<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ISE<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Description<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">This<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">determines<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">you<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">are<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">running<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Windows<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Powershell<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">ISE<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">by<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">querying<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$ExecutionContext<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">automatic<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">variable.<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Example<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Test-IsIseHost<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Prints<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">out<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">True<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">running<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">inside<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ISE,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">False<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">run<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">console<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Example<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(Test-IsIseHost)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> &#8220;Using the ISE&#8221; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Prints<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">out<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Using<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ISE<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">when<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">run<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">inside<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ISE,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">otherwise<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">nothing<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Inputs<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">None<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Outputs<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">[Boolean]<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Notes<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Name:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-IsIseHost<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Book:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Windows<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">PowerShell<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Best<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Practices,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Microsoft<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Press,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2009<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Author:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Ed<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Wilson<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Version:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Date:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #000000\">\/<\/span><span style=\"color: #800000\">5<\/span><span style=\"color: #000000\">\/<\/span><span style=\"color: #800000\">2009<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Link<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">about_Automatic_variables<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Http:\/\/www.ScriptingGuys.Com<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$ExecutionContext<\/span><span style=\"color: #000000\">.Host.name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-match<\/span><span style=\"color: #808080\"> &#8220;ISE Host$&#8221; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-IsIseHost<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Select-Destination(<\/span><span style=\"color: #2b91af\">$strIN<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Clear-Host<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">switch(<\/span><span style=\"color: #2b91af\">$strIN<\/span><span style=\"color: #000000\">.character)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/>&#8220;l&#8221; <\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> &#8220;$($strIN.Character): local selected&#8221; <\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">exit<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/>&#8220;r&#8221; <\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> &#8220;$($strIN.Character): Remote selected&#8221; <\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">exit<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">DEFAULT<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> &#8220;$($strIN.Character) is not valid. CountDown Resuming &#8230;&#8221; <\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">break}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Select-Destination<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Entry<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">point<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">to<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">script<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">If<\/span><span style=\"color: #000000\">(Test-IsIseHost)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&#8220;This script does not run in ISE&#8221; <\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">exit}<\/span><span style=\"color: #808080\"> <br \/>&#8220;Select machine:&#8221; <br \/>&#8221; l &lt;ocal&gt; r &lt;emote&gt; &#8221; <br \/><\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Do<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">Write-host<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ForeGroundColor<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">green<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-noNewLine<\/span><span style=\"color: #808080\"> &#8220;Script will time out in $($timer-$i) seconds&#8221; <br \/><\/span><span style=\"color: #2b91af\">$pos<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$host<\/span><span style=\"color: #000000\">.UI.RawUI.get_cursorPosition()<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$pos<\/span><span style=\"color: #000000\">.set_x(<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">This<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">was<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">syntax<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$pos<\/span><span style=\"color: #000000\">.X<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">this<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">is<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">syntax<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$host<\/span><span style=\"color: #000000\">.UI.RawUI.set_cursorPosition(<\/span><span style=\"color: #2b91af\">$Pos<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$host<\/span><span style=\"color: #000000\">.UI.RawUI.KeyAvailable)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Select-Destination(<\/span><span style=\"color: #2b91af\">$host<\/span><span style=\"color: #000000\">.ui.rawui.readkey())<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">start-Sleep<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Seconds<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[math]::log10(<\/span><span style=\"color: #2b91af\">$timer-$i<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[math]::truncate([math]::log10(<\/span><span style=\"color: #2b91af\">$timer-$i<\/span><span style=\"color: #000000\">))<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Clear-Host<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$i++<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">}While<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-le<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$timer<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Clear-Host<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\"> &#8220;Script timed out after $timer seconds&#8221; <\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">exit<\/span>\nKeep in mind that the pauseTimer.ps1 script does not run in the Windows PowerShell ISE. As a result, the <b>Test-IsIseHost<\/b> function is used. If the ISE host is found, the script will exit after displaying a message that the script does not run in the ISE host. The <b>Test-IsIseHost<\/b> function is shown here:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-IsIseHost<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">&lt;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Synopsis<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Determines<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">you<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">are<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">running<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Windows<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">PowerShell<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ISE<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Description<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">This<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">determines<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">you<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">are<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">running<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Windows<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Powershell<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">ISE<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">by<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">querying<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$ExecutionContext<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">automatic<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">variable.<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Example<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Test-IsIseHost<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Prints<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">out<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">True<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">running<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">inside<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ISE,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">False<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">run<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">console<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Example<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(Test-IsIseHost)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> &#8220;Using the ISE&#8221; <\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Prints<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">out<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Using<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ISE<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">when<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">run<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">inside<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">the<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ISE,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">otherwise<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">nothing<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Inputs<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">None<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Outputs<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">[Boolean]<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Notes<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Name:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-IsIseHost<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Book:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Windows<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">PowerShell<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Best<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Practices,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Microsoft<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Press,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2009<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Author:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Ed<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Wilson<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Version:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Date:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #000000\">\/<\/span><span style=\"color: #800000\">5<\/span><span style=\"color: #000000\">\/<\/span><span style=\"color: #800000\">2009<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">.Link<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">about_Automatic_variables<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Http:\/\/www.ScriptingGuys.Com<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">&gt;<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #2b91af\">$ExecutionContext<\/span><span style=\"color: #000000\">.Host.name<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Pausing execution of a Windows PowerShell script to receive keyboard input can be as simple as using the Read-Host cmdlet. But other methods are available. &nbsp; Hey, Scripting Guy! I would like to pause a Windows PowerShell script and wait for user input. When the user types a particular key on the keyboard, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,22,2,3,4,45],"class_list":["post-17451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-retrieving-input","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Pausing execution of a Windows PowerShell script to receive keyboard input can be as simple as using the Read-Host cmdlet. But other methods are available. &nbsp; Hey, Scripting Guy! I would like to pause a Windows PowerShell script and wait for user input. When the user types a particular key on the keyboard, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17451","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17451"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17451\/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=17451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}