Using PowerShell output notifications to flag items

Those of you who are familiar with my work know that I write a lot of PowerShell code. As time has gone on, I have found that the scripts that I write have inevitably become longer and more complex, and that the on-screen output can sometimes be cluttered and somewhat overwhelming. As such, it can be helpful to draw attention to key pieces of information within the output of a PowerShell script. By far the most common PowerShell output notification is an email message. However, if you are running a script and just want to draw attention to something important on the screen, generating an email message probably isn’t going to be the best way to accomplish that. I have often used three different techniques to use PowerShell output notifications items in my scripts to flag items.

Sound

Sometimes, all you need is a simple beep to get your attention. It’s really easy to embed a tone into a PowerShell script. Here is a command that you can embed at a strategic location within your script:

[console]::beep(1000,500)

In case you are wondering, the first of the two numbers (1000 in this case) controls the pitch of the tone, while the second number (500) controls the duration. You can adjust these numbers up or down to suit your own tastes.

Color

Another way of drawing attention to a particular item within your script’s output is by using color. Assigning a color to a line of text is as easy as adding the -ForegroundColor parameter to the Write-Host cmdlet, and then specifying a color. The valid colors include:

Black

Blue

Cyan

DarkBlue

DarkCyan

DarkGray

DarkGreen

DarkMagenta

DarkRed

DarkYellow

Gray

Green

Magenta

Red

White

Yellow

While you can often add color directly to your script’s existing commands, I personally prefer to use a function, because a function gives you a bit more flexibility. Here is an example:

This block of code is really simple. The script displays something not important on screen. Next, it adds the phrase “This is a notification” to a variable, and then passes that variable to a function called Display-Notification. That function displays the variable contents in green. When the function completes, the script writes something else unimportant but does so in the default text color (white).

Here is what the script’s output looks like:

Pop-up box

One more way that I sometimes notify myself of important items within a script is by creating a pop-up dialog box. There are countless ways of doing this, but the method that I like to use involves a graphical pop-up with an OK button. The reason why I like this method is because it actually pauses the script until you click OK to acknowledge the notification. I think that using sound or color definitely has its place, but you might miss a sound if your speakers are off, and a line of colored text might scroll off the screen before you can see it. The pop-up box, however, is really hard to miss. Here is the code:

In spite of the length of this script, it is actually very similar to the other script that I showed you. Like the previous script, this one writes something unimportant, and then adds the phrase “This is a notification” to a variable, and then passes the variable to a function called Display-Notification. This function displays the notification text (the variable contents) within a graphical pop-up until you click the OK button. At that point, the pop-up closes, and the script resumes. In this case, resuming the script causes something else unimportant to be written to the screen. Here is what the output looks like:

If you want to know how my script works in granular detail, then I recommend reading my article series on creating a PowerShell GUI. For right now, however, I will tell you that the pop-up is based on the use of a form. This particular form contains two elements.

The first of these elements is a label. The label displays the line of text contained in the $NotificationText variable.

The second element that is used on the form is a button. The button is labeled OK, and the button’s click action closes the form. When the form closes, the PowerShell window displays the word Cancel, and then resumes running the remainder of the script, as shown below.

One of the things that I have always loved about PowerShell is that it lends itself well to creativity. There are countless ways to draw attention to important items within a script’s output by using PowerShell output notifications. In this case, I have used sound, color, and graphical pop-ups, but certainly, these are not the only options. You could conceivably draw attention to specific parts of a script’s output in just about any way that you can imagine.