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.


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:


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.


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:

Function Display-Notification($NotificationText)
Write-Host $NotificationText -ForegroundColor Green
Write-Host 'Something not important'
$NotificationText = 'This is a notification'
Display-Notification $NotificationText
Write-host 'Something not important'

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:

Function Display-Notification($NotificationText)
#Load Assemblies
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
$net = New-Object -ComObject Wscript.Network
# -----Draw the empty form----
$Form = New-Object System.Windows.Forms.Form
$Form.width = 400
$Form.height = 200
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Form.Text = "Notification"
$Form.maximumsize = New-Object System.Drawing.Size(525,350)
$Form.startposition = "centerscreen"
#------ Define All GUI Objects-------
# Define Label - This is a text box object that will display the VM Name
$Label1 = New-Object System.Windows.Forms.Label
$Label1.AutoSize = $False
$Label1.Location = new-object System.Drawing.Size(20,50)
$Label1.Size = New-Object System.Drawing.Size(200,20)
$Label1.Text = $NotificationText
# Define Button 1 - This is the selection screen's OK button
$Button1 = new-object System.Windows.Forms.Button
$Button1.Location = new-object System.Drawing.Size(20,70)
$Button1.Size = new-object System.Drawing.Size(70,30)
$Button1.Text = "OK"
#----Populate the form----
Write-Host 'Something not important'
$NotificationText = 'This is a notification'
Display-Notification $NotificationText
Write-Host 'Something not important'

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.

Brien Posey

Brien Posey is a freelance technology author and speaker with over two decades of IT experience. Prior to going freelance, Brien was a CIO for a national chain of hospitals and healthcare facilities. He has also served as a network engineer for the United States Department of Defense at Fort Knox. In addition, Brien has worked as a network administrator for some of the largest insurance companies in America. To date, Brien has received Microsoft’s MVP award numerous times in categories including Windows Server, IIS, Exchange Server, and File Systems / Storage. You can visit Brien’s Website at:

Published by
Brien Posey
Tags Powershell

Recent Posts

Google adds better protection against man-in-the-middle attacks

Google is ramping up its security protections to better ward off man-in-the-middle attacks, which have caused extreme damage over the…

21 mins ago

‘Sea Turtle’ DNS hijacking attacks believed to be state-sponsored

A DNS hijacking campaign hitting governments and agencies in the Middle East and North Africa is believed to be the…

4 days ago

Top 9 cybersecurity startups to keep an eye on in 2019

Cybersecurity is one of the most heavily invested areas every year. Here are nine cybersecurity startups that are attracting the…

4 days ago

Is faxing still a viable mode of business communication? Oh, yeah!

Think the annoying squelch of a fax machine is an extinct sound from the past? Think again. For many businesses,…

4 days ago

Static IP address vs. dynamic IP address: Which is better for business?

While most businesses and home users can do quite well with a dynamic IP address, there are times you should…

5 days ago

Using MFA to connect to Exchange Online PowerShell

Exchange Online PowerShell is a great tool for managing Exchange Online in Office 365. But there are a few steps…

5 days ago