One-Liner: Countdown Timer in PowerShell

16 03 2008

Here’s a quick one for ya. Perfect kitchen timer. Who doesn’t have a laptop with PowerShell on it in their kitchen? :)

start-sleep (60*9); write-host ("`a"*4)

Start-Sleep works in seconds, so you see where I’ve done some quick
math to get nine minutes. Not sure if the ()’s were required, but they
don’t hurt. I didn’t know the precedence of parameter parsing versus
multiplication off the top of my head. Next is Write-Host with some
more multiplication. This one uses “`a” which is the special character
which emits a beep (old schoolers know this as the ASCII code for
BELL). If you multiply a string by a number in this way (and in this
order, actually), then posh concatenates it the number of times you
specify. Due to the magical parsing that PowerShell does, if you were
to try this the other way around, with the 4 first, you would get an
error when it tries to convert “`a” to an INT.

Happy Powershelling!


Actions

Information

2 responses

17 03 2008
Joel "Jaykul" Bennett

I wrote a full-featured one and put it on http://powershellcentral.com/scripts/161, which shows progress … and then I got carried away and added audio alerts and silly other stuff. Definitely not a one-liner, but your call to it would be! :-)

Start-Timer 90 "The Roast"

21 03 2010
Matt Duguid

Quick but does the trick…

$Countdown = 10
for($PercentComplete = $Countdown; $PercentComplete -ge 0; $PercentComplete–)
{
Write-Progress -Activity “Countdown” -PercentComplete $PercentComplete -Status “Counting down, press CTRL + C to kill this script now if required!”;Sleep -Seconds 1;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

Join 916 other followers