Friday Fun: A PowerShell Nap

antique-watch-150x225I’m hoping that I’m not the only one who feels their butt dragging by mid to late afternoon. Let’s say that’s because we’ve been thundering through the day and by 3:00 we’re a bit out of gas. Yeah, I’ll go with that.

I find myself wanting to close my eyes for only a few minutes to recharge and or at least take the tired edge off.  In other words, I just want a quick nap like we had in kindergarten.  But I need to make sure I wake up! Since I always have a PowerShell console, I can use it as a quick and dirty alarm clock.

For starters I can use the Start-Sleep cmdlet to wait for X number of seconds. So if I want say a 10 minute timer I can run:

But if my eyes are closed how will know when time is up? A quick solution is to make my computer beep using the [console] .NET class.

So to run this all at once I can enter a command like this:

The semi-colon is the end of command marker so what I’m really doing here is executing 2 commands. PowerShell will sleep for 600 seconds and when that command completes, then the Beep() method will run.

But of course it is Friday so let’s have a bit more fun with this.

The Beep() method can also take 2 parameters. The first is a tone frequency, and the second is a duration in milliseconds.

With this in mind I put together a PowerShell napping script.

The script takes a parameter for the number of minutes you need to nap. It then writes a message to anyone who might be walking by your desk to keep quiet.  The script then does a countdown of sorts by calculating a timespan between the target end time and the current time. I’m writing the value as a string so that I can strip off the milliseconds.

nap in progress

nap in progress

At the end of the countdown I’ve recreated a well known chime, at least for those of you in the United States and of a certain age. I’ll let you try it out for yourself.

nap complete

nap complete

So get a bit more work done today,  and when you’re ready, take a quick PowerShell Power nap.

Enjoy and sweet dreams.

3 thoughts on “Friday Fun: A PowerShell Nap

  1. It’s a nice project, but why use the pc speaker?
    I would use .net text to speech so I can just put on my headphones and wake up to a voice, instead of the entire office listening to the pc speaker 😉
    Something like this:
    Add-Type -AssemblyName System.speech
    $Speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
    $InstalledVoices = $Speak.GetInstalledVoices() | ForEach-Object { $_.VoiceInfo }
    $InstalledVoices | ForEach-Object { If($_.Name -eq “Microsoft Hazel Desktop”) { $Speak.SelectVoice(“Microsoft Hazel Desktop”) } }
    $Speak.Speak(‘Wake up and assimilate the day. We are the borg. Resistance is futile!’)
    $Speak.Speak(‘Wake up and assimilate the day. We are the borg. Resistance is futile!’)
    Start-Sleep -Seconds 1
    $InstalledVoices | ForEach-Object { If($_.Name -eq “Microsoft David Desktop”) { $Speak.SelectVoice(“Microsoft David Desktop”) } }
    $Speak.Speak(“Okay, but how do I turn you off?”)
    Start-Sleep -Seconds 1
    $InstalledVoices | ForEach-Object { If($_.Name -eq “Microsoft Hazel Desktop”) { $Speak.SelectVoice(“Microsoft Hazel Desktop”) } }
    $Speak.Speak(“Believe me, I’m pretty much turned off right now.”)
    Start-Sleep -Seconds 2
    $InstalledVoices | ForEach-Object { If($_.Name -eq “Microsoft David Desktop”) { $Speak.SelectVoice(“Microsoft David Desktop”) } }
    $Speak.Speak(“Time to get back to work and replace someone with a very small shell script!”)

    • That would be a terrific enhancement! I work at home alone so I don’t worry too much about bothering anyone.

  2. Pingback: Text to speech alarm clock with powershell | Powershell Administrator Blog

Comments are closed.