Home QuickTipps Correctly Showing Balloon Tips
 

Correctly Showing Balloon Tips

There are numerous script samples out there showing how to display a balloon tip in the taskbar notification area – which can be useful to show unobtrusive messages from script.

However, many of these samples do not fully understand object scope, so when you run the code multiple times, you end up with a number of polluting tooltip icons in your notification area.

So let's check out how PowerShell can actually display notfication icons – and get rid of them again.

Displaying Balloon Tooltip

Here is a function that easily displays a balloon tooltip.

  

function Show-BalloonTip  
{
 
  [CmdletBinding(SupportsShouldProcess = $true)]
  param
  (
    [Parameter(Mandatory=$true)]
    $Text,
   
    [Parameter(Mandatory=$true)]
    $Title,
   
    [ValidateSet('None', 'Info', 'Warning', 'Error')]
    $Icon = 'Info',

    $Timeout = 10000
  )
 
  Add-Type -AssemblyName System.Windows.Forms

  if ($script:balloon -eq $null)
  {
    $script:balloon = New-Object System.Windows.Forms.NotifyIcon
  }

  $path                    = Get-Process -id $pid | Select-Object -ExpandProperty Path
  $balloon.Icon            = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
  $balloon.BalloonTipIcon  = $Icon
  $balloon.BalloonTipText  = $Text
  $balloon.BalloonTipTitle = $Title
  $balloon.Visible         = $true

  $balloon.ShowBalloonTip($Timeout)

  Show-Balloon displays a balloon tooltip in your taskbar notification area

After running the function, simply use it like this:

PS> Show–BalloonTip –Text 'Script has retrieved data' –Title 'All is fine' –Icon Info –Timeout 5000

PS> 

If there is no balloon tip showing up, then balloon tips may have been disabled on your system.

The balloon tip shows the title and message you provided, and also uses the icon you specified. Inside the notification area, there is another icon that the function takes from your application – so it is different, depending on whether you run the function from within the ISE editor, the powershell console, or another Powershell host.

No Multiple Toolbar Icons

You can run Show-BalloonTip multiple times, as often as you wish. The icon will reappear, show new text, then go away again. There are no multiple icons. The function is reusing the same tray icon.

And that is what most sample scripts do wrong. Each time you create a new NotifyIcon object, a new icon is generated. So your function would have to check whether there is already an icon that you can reuse. This is done by using a "shared variable", which really is a variable that has "script:" scope.

Shared variables will keep their value as long as the script runs (so in this case, as long as your PowerShell session runs). Shared variables will not go out of scope when a function is done.

Getting Rid Of Toolbar Icon

The one toolbar icon that is generated will hang around as long as PowerShell runs. It may hide in the hidden icon tray so you have to click the arrow-up icon in your notification bar to see it, but it is still there.

You have to call its method Dispose() to actually remove it from the tray bar. So you can do this from script:

PS> $script:balloon.Dispose()

PS> 

  Getting rid of toolbar icon

The icon will immediately be removed from the tray area. But when you try and show another balloon, you will get an error now – the function just checks whether there is a shared variable called "balloon" and tries and reuses the icon.

Since you have disposed it, it no longer works. So when you do want to dispose the tray icon, make sure you also dispose your variable:

PS> $script:balloon.Dispose()

PS> Remove–Variable –Scope script –Name balloon

PS> 

  Make sure you also dispose the shared variable

Cleaning Up After Yourself

So if you do use toolbar icons and balloon tips, make sure you clean up after your script and dispose the toolbar icon you created.

If you do not, the icon will stay in the toolbar even after your PowerShell script ended. Since it has no attached application anymore, when the user hovers over the abandoned icon with the mouse, only then will windows detect that there is no one caring about this icon anymore, and it vanishes.