Techibee.com

System tray pop-up message notifications using PowerShell

In this post, I will show you how to get pop-up windows in System tray notification area using balloon tips and Powershell.

If you are looking for a article about how to get pop-up message box using powershell rather than balloon tip, refer to this article: https://techibee.com/powershell/how-to-get-pop-up-message-box-using-powershell/839

System tray notifications increase the user experience. Say I am  a Powershell script which will shutdown a server and wait for it to come online, I don’t have to keep watching the console to know if my server is up or not. Rather I would rely on some notification (email, pop-up messages, etc) to get the status so that I can work on something else while my server(s) is booting up. It sounds interesting. Isn’t it?

We can list down “n” number of use cases where System tray notifications will come handy in various scripts. I won’t list all of them here as it is impossible and I will leave it to your imagination to get your own list of use cases. However, what I will cover in this post is, the code used for generating system tray notifications using balloon tips. This post is actually inspired from today’s daily tip from http://powershell.com. They have given a basic code using which you can get balloon pop-up message. Here I will give you a sophisticated function which you can use in any script to provide different types of messages.

Function: Show-BalloonTip

function Show-BalloonTip {            
[cmdletbinding()]            
param(            
 [parameter(Mandatory=$true)]            
 [string]$Title,            
 [ValidateSet("Info","Warning","Error")]             
 [string]$MessageType = "Info",            
 [parameter(Mandatory=$true)]            
 [string]$Message,            
 [string]$Duration=10000            
)            

[system.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null            
$balloon = New-Object System.Windows.Forms.NotifyIcon            
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path            
$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)            
$balloon.Icon = $icon            
$balloon.BalloonTipIcon = $MessageType            
$balloon.BalloonTipText = $Message            
$balloon.BalloonTipTitle = $Title            
$balloon.Visible = $true            
$balloon.ShowBalloonTip($Duration)            

}

Usage instructions:

Show-BalloonTip -Title “my message” -MessageType Warning -Message “you have a warning dude” -Duration 1000

Show-BalloonTip -Title “Error occurred” -MessageType Error -Message “you have a Error dude” -Duration 1000

Sample Output:

Hope you will find this function useful. Feel free to use it in your scripts.

I welcome your comments for improvisation.

Exit mobile version