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

Friday Fun: A PowerShell Nap

Posted on January 22, 2016

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.

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!

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:

start-sleep –seconds 600

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.

[console]::Beep()

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

start-sleep –seconds 600 ; [console]::Beep()

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.

[console]::Beep(440,1000)

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

#PowerShellNap.ps1

#nap time in minutes
Param([int]$Naptime = 1)

#define the wakeup time
$wake = (Get-Date).AddMinutes($Naptime)

#loop until the time is >= the wake up time
do {
cls
Write-host "Ssshhhh...."

#trim off the milliseconds
write-host ($wake - (Get-Date)).ToString().Substring(0,8) -NoNewline

Start-Sleep -Seconds 1

} Until ( (Get-Date) -ge $wake)

#Play wake up music
[console]::Beep(392,1000)
[console]::Beep((329.6*2),1000)
[console]::Beep(523.2,1000)

Write-Host "`nBack to work sleepy head!" -ForegroundColor Yellow

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.

C:\scripts\PowerShellNap.ps1 –naptime 10

Enjoy and sweet dreams.


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

3 thoughts on “Friday Fun: A PowerShell Nap”

  1. PowershellAdministrator says:
    January 25, 2016 at 8:06 pm

    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!”)

    1. Jeffery Hicks says:
      January 25, 2016 at 9:41 pm

      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.

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