Why we need Balloon Tip Notification?

Mostly you will use common notification like display message in console using Write-* cmdlets to write the text to the console and Commonly we use Write-Error or Write-Warning for displaying Error and warnings.

Apart from displaying the message through the console, sometimes we required to get the user’s attention when something has completed or an issue is encountered. In that case, a pop-up window will helps you to built and displayed to the information/Warning/Error to the User. In this type of situation, we can implement the Balloon Tool Tip notification.

To achieve this, we need to work with the System.Windows.Forms.NotifyIcon type to build our notification balloon tip.

STEP 1# :

To get started, you need to load up an assembly to take advantage of a method that will help to extract the icon image from the file.

Add-Type -AssemblyName System.Windows.Forms

STEP 2# :

Second, you need to add the System.Windows.Forms assembly into our PowerShell session before we can make use of the NotifyIcon class. 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 be active their value as long as the script runs.

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

STEP 3# :

Then you want to set the system tray icon of the PowerShell ISE by just locating its path via Get-Process and locating the Path property. After you have the path, then you need to extract the icon from the file so it can be applied to the Icon property.Using this approach, you can pull any icon from a file and use it for the system tray icon when the balloon tip is launched.

As per above statement, the Get-Process -id $pid command gets the PowerShell process that is hosting the current session.The ExpandProperty expands the collections, that is, it will output the properties of every object in the collection. For example the out of $path variable will be “C:\windows\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe”

$path = Get-Process -id $pid | Select-Object -ExpandProperty Path

STEP 4# :

The ExtractAssociatedIcon which comes with the System.Drawing.Icon class and accepts a parameter of a string path name.

$balloonToolTip.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)

Full code

OUTPUT: