Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

The PowerShell Balloon Festival

Posted on November 13, 2009November 13, 2009

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

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.

Function Show-Balloon {

#requires -version 2.0

<#

.Synopsis

    Display a balloon tip message in the system tray.

.Description

    This function displays a user-defined message as a balloon popup in the system tray. This function

    requires Windows Vista or later.

.Parameter Message

    The message text you want to display.  Recommended to keep it short and simple.

.Parameter Title

    The title for the message balloon. The default is Attention: $env:username, where the variable

    will be replaced by the %USERNAME% environmental variable.

.Parameter MessageType

    .The type of message. This value determines what type of icon to display. Valid values are

    Error, Warning, Info and None. The default is Info. This paramater has an alias of 'type'.

.Parameter Duration

    The number of seconds to display the balloon popup. The default is 10.

.Example

    PS C:\>  Show-Balloon "Your script has finished." -duration 15 -Title "Hey, You!!"

    Displays a simple balloon popup for 15 seconds.

.Example

    PS C:\>  Show-Balloon "There was an error with the script: $error[0].Exception.Message" -messagetype "Error"

    Display an error balloon tip showing the exception message from the last error PowerShell detected.

.Example

    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.

.Inputs

    None

.Outputs

    None

.Link

   https://jdhitsolutions.com/blog

.Link

    Write-Warning

    Write-Error

.Notes

 NAME:      Show-Balloon

 VERSION:   1.0

 AUTHOR:    Jeffery Hicks

 LASTEDIT:  11/12/2009

#>

[CmdletBinding()]

Param (

    [Parameter(

     ValueFromPipeline=$False,

     Position=0,

     Mandatory=$True,

     HelpMessage="The message text to display. Keep it short and simple.")]

     [string]$message,

    [Parameter(

     ValueFromPipeline=$False,

     Mandatory=$False,

     HelpMessage="The message title")]

     [string]$Title="Attention $env:username",

    [Parameter(

     ValueFromPipeline=$False,

     Mandatory=$False,

     HelpMessage="The message type: Info,Error,Warning,None")]

     [ValidateSet("Info","Error","Warning","None")]

     [Alias("type")]

     [string]$MessageType="Info",

     [Parameter(

     ValueFromPipeline=$False,

     Mandatory=$False,

     HelpMessage="The number of seconds to display the message.")]

     [int]$duration=10

     )

    Write-Verbose "Starting Function"

    if ($verbosePreference -eq "Continue") {

        Write-Verbose "Getting Operating System"

        Write-Verbose "OS: $((get-wmiobject win32_operatingsystem).caption)"

     }

    Write-Verbose "Loading Assemblies"

    #load Windows Forms and drawing assemblies

    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null

    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

    #define an icon image pulled from PowerShell.exe

    Write-Verbose "Extracting the icon from PowerShell.exe"

    $icon=[system.drawing.icon]::ExtractAssociatedIcon((join-path $pshome powershell.exe))

    Write-Verbose "Defining `$notify"

    $notify = new-object system.windows.forms.notifyicon

    $notify.icon = $icon

    $notify.visible = $True

    Write-Verbose "Evaluating MessageType parameter $messagetype"

    #define the tool tip icon based on the message type

    switch ($messagetype) {

     "Error" { $messageIcon=[system.windows.forms.tooltipicon]::Error}

     "Info" {$messageIcon=[system.windows.forms.tooltipicon]::Info}

     "Warning" {$messageIcon=[system.windows.forms.tooltipicon]::Warning}

     Default {$messageIcon=[system.windows.forms.tooltipicon]::None}

    }

    Write-Verbose "Displaying message [ $Message ] for $duration seconds"

    #display the balloon tipe

    $notify.showballoontip($duration,$Title,$message,$MessageIcon)

    Write-Verbose "Ending function"

} #end function

Set-Alias sb 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


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

3 thoughts on “The PowerShell Balloon Festival”

  1. Pingback: Tweets that mention The PowerShell Balloon Festival | The Lonely Administrator -- Topsy.com
  2. Joel Bennett says:
    November 15, 2009 at 5:16 pm

    Hey, you should check out Growl for stuff like this, it’s so much more flexible than systray balloons.

    1. Jeffery Hicks says:
      November 16, 2009 at 8:19 am

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

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d