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")
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.
Even better, there is an open source PowerShell
I appreciate the culmination of information on displaying messages. Sometimes you’ll find a reference to one or two but depending upon what you are doing it is good to know there are more options. I do like the balloon method most for the only reason that it is a visual notification but does not interfere with what may be currently doing at the time. But I’m sure you could use a combination based on the importance of the PS process running.
Again, great article!
Thanks!