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.
Comments on this entry are closed.
How would you push these to remote machines on the same domain?
I love the code and is almost exactly what I need. its seems that it leaves the icon in the tray and after each balloon pop up, leaves another. How would you keep this clean each time? I have some long running scripts that would benefit from this cmdlet.
Thanks,
use:
$balloon.dispose()
Where does $balloon.dispose() go?
After the $balloon.ShowBalloonTip() method is called:
$balloon.ShowBalloonTip($Duration)
Start-Sleep -Milliseconds $Duration
$balloon.Dispose()
Start-Sleep is needed so as not to dispose of the object too quickly. Unfortunately this also blocks the function.
This is a fantastic tip. I’m using this function with a countdown timer, and it’s super-useful.
This is a great article! Thanks for taking the time to write this up it saved me a lot of time and was exactly what I was looking for!
How can we increase duration, Setting Duration is not impacting , balloon is getting disappear after few sec only.
Duration is in milliseconds. Make sure to pass the number accordingly. 20000 is the value if you want to have the pop-up for 20 seconds.
Thank you
is there a way to change the size of the notification bubble itself? I was looking to change the size of the password expiration bubble for our users.
Hi, I haven’t come across any approach that can change the bubble size. This seems windows default.
I have saved the script in a .ps1 file, but nothing happens when I run it.
Hi,
Copy paste the code into powershell window and try the usage instructions mentioned in the blog. Let me know if it still fails.
Hi everyone,
How would you push these to remote machines on the same domain?
i would like to use that function to notify users
thks for the script
Sending pop-ups to remote machines is tricky as this is interactive behaviour. Let me test and post a new article about it.
I’m interested in ending to remote machines on a domain as well. Have you made any progress? I do know it is difficult because remote processes are usually put into another user space even if the credentials are the same.
sending not ending, lol
No solution is developed for this requirement. However it is possible. I have seen many people asking for this information. Stay tuned for it. I will make a post on it soon.
I have been trying a few things too but unable to have it run in the logged on user session on the remote system. I sincerely hope you are making better progress.
Nice program