PowerShell Show-BalloonTip Function

How PowerShell Can Display a Notification BalloonPowerShell Show-BalloonTip Function

This is just a fun project to learn about PowerShell functions; in particular, how to make use of .Net objects such as Windows.Forms.

Show-BalloonTip Function

 ♣

Planning the Show-BalloonTip Function

The key object for this function is: System.Windows.Forms.NotifyIcon.  If you applied Get-Member it would confirm ShowBalloonTip to be a method, and BalloonTipText to be one of about a dozen properties.

Get-Process lists all running processes together with their PIDs. The significance of $PID is that contains the identifier the current PowerShell session.

As usual with a PowerShell function, there is a Parameter section at the top and a Process section at the heart of the function.

Code for the Show-BalloonTip Function

The purpose of this function is to purely to display text from the PowerShell icon in the notification area.

Function Global:Show-BalloonTip {
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)]$Text,
[Parameter(Mandatory=$true)]$Title,
$Icon = 'Info',
$Timeout = $10000
         )
Process {
Add-Type -AssemblyName System.Windows.Forms
If ($PopUp -eq $null)  {
       $PopUp = New-Object System.Windows.Forms.NotifyIcon
                    }
         $Path = Get-Process -Id $PID | Select-Object -ExpandProperty Path
         $PopUp.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($Path)
         $PopUp.BalloonTipIcon = $Icon
         $PopUp.BalloonTipText = $Text
         $PopUp.BalloonTipTitle = $Title
         $PopUp.Visible = $true
         $PopUp.ShowBalloonTip($Timeout)
                 } # End of Process
} # End of Function

Example of This Function in Action: PowerShell Show-BalloonTip Function
Show-BalloonTip -Text 'Look at me!' -Title 'Tip from Guy'

Note 1: Observe the two mandatory parameters: $Text, which translates to -Text, and also $Title.

Note 2: $Timeout = Sets the time that the balloon appears in milliseconds.  The default in this script $10000 is ten seconds.

Note 3: $PID is process identifier for the program that runs the script, and that would be PowerShell.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Researching Windows.Forms Properties

To discover more about the available properties and methods I employed one of my favourite research cmdlets: Get-Member.PowerShell Show-BalloonTip Function

# Research NotifyIcon properties and methods
Clear-Host
$Script = New-Object System.Windows.Forms.NotifyIcon
$Script | Get-Member

Note 4: This was how I discovered the BalloonTip family of properties, and also the spelling of the 'ShowBalloonTip() method.

Note 5: My function uses $Icon = 'Info', however you could create 'Warning' or 'Error' notification balloons.

(Note 6: I cannot get $Script:PopUp.Dispose() do what I want.)

Variation of Show-BalloonTip

This variation is more suitable for testing, the parameters are declared as positional with default values, thus you don't need to explicitly state the title and text of the balloon notification.  Another difference is this example specifies the 'Error' icon rather than the 'Info'.

Function Global:Show-BalloonTip {
[CmdletBinding()]
Param (
[Parameter(Position=0)]$Title='Problem',
[Parameter(Position=1)]$Text ='Check all stuff',
[Parameter(Position=2)][int]$Timeout = 1,
$Icon = 'Error'
          )
Process {
Add-Type -AssemblyName System.Windows.Forms
If ($PopUp -eq $null) {
      $PopUp = New-Object System.Windows.Forms.NotifyIcon
                     }
          $Path = Get-Process -Id $PID | Select-Object -ExpandProperty Path
          $PopUp.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($Path)
          $PopUp.BalloonTipIcon = $Icon
          $PopUp.BalloonTipText = $Text
          $PopUp.BalloonTipTitle = $Title
          $PopUp.Visible = $true
          $PopUp.ShowBalloonTip([int]1)
     } # End of Process
} # End of Function
Show-BalloonTip

See more PowerShell functions »

Summary of Show-BalloonTip Function

Here is a trivial example of how to create a PowerShell function that interacts with the notification area.  What's interesting is the way it access Windows.Forms.

If you like this page then please share it with your friends

 


See More PowerShell Function Parameters

Scripting PowerShell Function   • PowerShell Function Clear-Recyclebin   • PowerShell Get-File

Create PowerShell Function Get-ServiceStatus  • PowerShell Function Get-Driver  • PowerShell Outlook

Show-BalloonTip   • PowerShell Function Get-IPConfig   • Free Permissions Analyzer

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.