Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Press PowerShell Pause to Continue

Posted on August 8, 2014August 8, 2014

talkbubble Everyone once in a while I come across a PowerShell script that behaves like an old-fashioned batch file. Not that there's anything wrong with that, but often these types of scripts put in a pause at the end of script so you can see that it finished. You might have seen a command like this in a PowerShell script.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!
cmd /c pause

Sure, it works but is hardly elegant PowerShell. First, I'm not sure you even need it. If you want to draw attention to the fact that your script ended, consider using Write-Host with a foreground (or background color).

Write-Host "Press any key to continue . . ." -foregroundcolor yellow

But if you truly need the user to acknowledge that the script has finished you can use Read-Host.

Read-Host "Press any key to continue . . ." | Out-Null

I'm piping the command to Out-Null because I don't care what the user enters, only that they hit a key.

If you prefer a move visual cue, you can always resort to the Wscript.Shell COM object.

$ws = new-object -ComObject wscript.shell
$ws.Popup("Press any key to continue . . .",0,"Script Finished",0+64) | Out-Null

The popup will remain until the user clicks OK. Or you can change the second parameter from 0 to another value to have the popup automatically dismiss after the specified number of seconds.

So if you need a refreshing pause, there are plenty of PowerShell options for you to use.

UPDATE:
After someone posted a comment I realized there is yet another way to do this. Add these two lines to the end of your PowerShell script.

write-host `n"Press any key to continue . . ." -NoNewline -ForegroundColor Green
$host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown,IncludeKeyUp") | out-null

This is very similar behavior to Read-Host but with a bit more flexibility.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

8 thoughts on “Press PowerShell Pause to Continue”

  1. laurelraven says:
    August 8, 2014 at 4:28 pm

    With “Read-Host”, though, wouldn’t that not actually dismiss until you hit enter? So, “Press Enter to continue…”?

    1. Jeffery Hicks says:
      August 8, 2014 at 4:41 pm

      You’re right. To be clear you whould change the text which I assumed people would anyway. I was merely re-using the same text.

  2. James Brown says:
    August 11, 2014 at 9:31 am

    Why is Write-Host so verboten in the PowerShell community but Read-Host is acceptable?

    1. Jeffery Hicks says:
      August 11, 2014 at 9:55 am

      Because people use Write-Host instead of writing to the pipeline. Read-Host is completely different.

  3. Craig Urquhart says:
    August 11, 2014 at 10:38 am

    We started to use the ReadKey method (without the IncludeKeyUp option) until we found out that switching windows using alt-tab can cause this to trigger – even though you’re not ready for it. We switched to reading the value and making sure it complies with that we’re looking for in a do while loop. I’ll need to try adding the IncludeKeyUp to see if that makes a difference.

    1. Jeffery Hicks says:
      August 11, 2014 at 12:22 pm

      Interesting. I have not run into that situation. Thanks for sharing.

  4. Sam Gordon says:
    September 12, 2014 at 11:02 am

    Powershell 3.0 has the “Pause” command.

    1. Jeffery Hicks says:
      September 12, 2014 at 12:05 pm

      Why yes it does and the code is essentially the same as mine in terms of using Read-Host. My approach has a few more bells if you need them. But thanks for pointing this out.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d