The PowerShell Balloon Festival

I trust by now you are realizing how valuable Windows PowerShell  is as a management tool. With a one line command you can accomplish an extraordinary amount of work. Sometimes this work may be long running, which is where background jobs come in handy. Or you may simply kick off a long running script and go about your other administrative tasks. Unfortunately, you have to keep stopping what you’re doing to check and see if your script or job has finished. But there is a better way, assuming you are running Windows Vista or later.

When Don Jones was at SAPIEN Technologies, I believe he wrote a blog entry about creating a pop balloon notification in the system tray. This handy system could be used to notify the logged on user just of about anything. I took the liberty of revising the function and turned it into a PowerShell v2.0 function called Show-Balloon.




The function requires PowerShell version 2 and Windows Vista or later. Although it doesn’t check your operating system. I always like providing help information so the function can be used like a cmdlet.

show-balloon

balloontip

The function displays a message in a popup balloon by default for 10 seconds. You can customize the duration, title and the icon. The icon is determined by the MessageType parameter and can be Error, Warning, Info or None.

The function is relatively simple using the Drawings and Forms .NET classes to create and display a NotifyIcon object.

You might use this function to display a message after a script has finished.

PS C:\>  c:\scripts\myscript.ps1;Show-Balloon “Your script has finished.” -duration 15 -Title “Hey, You!!”

By using the ; to indicate a new line I can type a one line command and then move on to something else. You can easily incorporate variables into the message.

PS C:\> MyFunction ; Show-Balloon “There was an error with the script: $($error[0].Exception.Message)” -messagetype “Error”

Or use it with Invoke-Command.

PS C:\> Invoke-command -computer $env:computername -scriptblock {. c:\scripts\show-balloon.ps1;get-eventlog -logname system -newest 500;show-Balloon “Background job complete.”} -asjob

This example uses Invoke-Command to execute a potentially long running expression on the local computer. When  it is finished, a balloon message is displayed. Because the job runs in a separate scope, the script containing  the Show-Balloon function is dot sourced. After the Get-Eventlog cmdlet finishes, then the Show-Balloon function  is run, displaying the message.

I hope you’ll let me know how you use this function.
Download Show-Balloon.ps1

3 thoughts on “The PowerShell Balloon Festival”

    1. I’ve been meaning to try that out. The balloon trick is quick and dirty and doesn’t require anything.

Comments are closed.