4 Types of Notifications Generated in PowerShell

When a PowerShell script takes long time to finish, it’s nice to have some sort of notification set up in place to notify you when the job is finished.

Here are three type of notifications you can create in PowerShell.

Notify via Email

PowerShell has a native cmdlet Send-MailMessage that can be easily set up and use. I have written this post early this year that covers the basics of it with an example.

Notify via a beep

Adding the following line at the end of your script and it will make a beep when the job is finished.

[console]::beep(2000, 1000)

You will need two numbers for the beep to work. The first number controls the pitch of the tone while the second number controls the duration. You can adjust these numbers to suit your own cases.

Notify via a message box

There are a number of ways that you can use to pop up a message box in PowerShell. Here is one that uses the script host object that looks fairly easy to use and engage. Thanks to Shaun Ritchie.

$wshell = New-Object -ComObject Wscript.Shell 
$Output = $wshell.Popup("The task has finished")
PowerShell dialog box - 4 Types of Notifications Generated in PowerShell

You can explorer more properties of the Popup box to stylize the message box.

Notify by balloon tip / toast notification

The balloon tips comes to us since Windows 7 and can also be programmed in PowerShell. This is probably the most fun way to deliver a notification among other types we just covered.

You can follow this awesome how-to write up by Boe Prox on MCP Mag to generate one balloon tip for your own.

For starters, here is the code:

Add-Type -AssemblyName System.Windows.Forms 
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balloon.BalloonTipText = 'What do you think of this balloon tip?'
$balloon.BalloonTipTitle = "Attention $Env:USERNAME"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)

That generates the following balloon tip popup that lasts for 5 seconds.

PowerShell balloon tip - 4 Types of Notifications Generated in PowerShell

Even better, there is an open source PowerShell module called BurntToast that you can use to generate balloon tips with a lot more options.

Leave a Reply

Your email address will not be published. Required fields are marked *