Friday Fun: Improved PowerShell Napping

So I had some fun with my post last week on taking a nap with PowerShell. I got some great feedback on Twitter and a new comments on the blog. My initial effort was a relatively simple PowerShell script which certainly got the job done. But I there were a number of areas where I could expand and improve the script and they would be terrific teaching aids. So I did.

The function is defined in a script you can find in Github.

Let’s look at some of the changes I made. First off, I turned this into a function complete with comment based help. You’ll need to dot source the script file into your PowerShell session or profile script to make the command available.

I made just about every option a parameter and added a few parameter aliases as well. So even though I made the Minutes parameter positional so that you don’t need to use the parameter name, you could use –Nap or –Time. You’ll notice I also made the wakeup message a parameter.  Feel free to set your own default value. Otherwise, you can set a different message at different times.

I also realized that if you are napping, someone might still drop by your desk. So I included an option to display a progress bar using Write-Progress. This is a cmdlet that doesn’t get the love it should.

I defined an array of messages:

The messages will be used as the Status property for Write-Progress. I like using a hashtable of parameters to splat when using Write-Progress.

If I use the Progress bar, it is displayed using the seconds remaining.

And every 10 seconds I set the status to another randomly selected message. The result is something like this:


The last major change I made per a suggestion was to use the text to speech feature to have a Windows voice “say” the wake up message. I added a parameter for you to specify a voice name which in the US will most likely be David or Zira.  If you don’t know the names, you can specify a bogus value like ‘foo’ and the function will display the available names. This works because I added a validation script to the Voice parameter.

This is probably a bit more involved than most validation scripts.  The main takeaway is that if you use a validation script it has to return either True or False, or throw an exception as I’m doing here. But it works.


By adding a voice option I decided the function could either display the message using Write-Host or speak it. The chime happens in either event.

What this meant was that I had to differentiate the parameters which I did with parameter sets.  I specified the default in the cmdletbinding attribute.

Then I needed to specify a parameter set name for each parameter.  If you don’t specify parameter set name, then the parameter will belong to all sets.  Or you can do as I did and be explicit. If you do it properly it should be reflected in the help.


You can see that there are 2 ways to use this command. I’ll let you grab a copy and try out the new additions.

Certainly this isn’t a production oriented script but I hope it serves up some interesting examples of different scripting techniques and cmdlets.

As always, comments sincerely welcomed.


Friday Fun Perf Counters with Write-Progress

While working on my course for TrainSignal on managing Windows Servers with PowerShell, I came up with an interesting use of the Write-Progress cmdlet. I was working on the performance monitoring lesson and realized I could use Write-Progress as rudimentary graphing tool. The cmdlet has a parameter that lets you specify a value that shows up as the progress bar. Normally we think about moving from 0 to 100, but you can specify any value. So I thought why not pass a performance counter value? After a little work I came up with this demo script. Continue reading

Test Port 2.0

A few years ago I updated a PowerShell script I came across to scan a computer for open ports. My initial revision was aimed at making it more pipeline friendly in PowerShell v1.0. I recently needed to use the function for a project and realized it could benefit from a 2.0 upgrade. Continue reading

New Event Report

For a number of years I wrote the popular Mr. Roboto column for REDMOND magazine. When I first started the column, many of my scripts were written in VBScript. Then as PowerShell came along that became the preferred tool. Over time I realized there were some VBScripts that could be rewritten and even improved using PowerShell. One of them was a script that queried event logs on computers for errors and warnings in the last X number of hours and prepared a colorized HTML report. I revised that script to a PowerShell v1 script. Recently, a loyal reader asked about revising the script again for PowerShell 2.0. That seemed reasonable so here is my revision. Continue reading