A PowerShell Countdown Timer

The other day, during one of the monthly #PSTweetChat sessions, I exchanged some tweets with Joshua King. We got on the topic of countdown timers and he shared some code he uses for his YouTube channel. The command creates a progress bar and counts down, displaying some humorous messages along the way. There’s absolutely nothing wrong with his solution but I can’t help myself and am always looking to see what else it can do.  So after a bit of time with it, I came up with a version I call Start-PSCountdown. This is something I expect to be able to use in my PowerShell training classes and conferences.

My version, which should work on Windows PowerShell and PowerShell Core, accepts a few parameters. You can set the timer for anywhere between 1 and 60 minutes. The default is 5. You can also set a title for the progress bar and a countdown message. The rest of the display is randomly drawn from a list fun messages. I’ve added a few more in my version. I also added a dynamic parameter for something I call ProgressStyle. If PowerShell detects that you are using a Windows console, you will see the parameter. The downside with dynamic parameters is that you don’t get tab completion or discovery with PSReadLine. The default behavior is to use the current setting for $host.PrivateData.ProgressbarBackgroundColor. But you can use a parameter value of Transparent which will set the progress bar background to the same color as the console, sort of making it transparent. Or you can use –Random and you’ll get a random background color from a subset of console colors.

Note that as of today, the integrated PowerShell host in VS Code does not support Write-Host so you’ll get a warning message.

The code for my function is on GitHub.

If you have suggestions or problems, please post a followup comment on the page.

I hope you’ll let me know what you think and a big thanks to @WindosNZ.

5 thoughts on “A PowerShell Countdown Timer

  1. Interestingly, it’ll work in VS Code as long as it’s running in “powershell” and not the Integrated Console that’s part of the PowerShell extension. (Example: http://iforce.co.nz/i/x5kzuekb.x5g.png)

    Handy to know if you’re determined to only use Code during demos/presentations (I really need to break the ISE habit).

    Thanks for the awesome write up, script, and shoutout!

    • I only use the integrated console in VSCode. But that’s fine as I never use VSCode as a demo tool unless I’m demoing VSCode! The ISE is still a better presentation tool.

    • There’s really no end to this foolishness. I’m considering a version where the function would import from an external file. That would be the smart way to go and keep the function code a bit more manageable.

Leave a Reply

Your email address will not be published. Required fields are marked *