You’ve probably seen those balloon pop-ups at your computer at some point in time. Most likely it was saying something about having updates that need to be installed on your computer. They pop-up and say something and you can either click on it to bring up another window, close it out or let it disappear and continue on your merry way. One of the many things that Powershell can do is create the same type of pop-up using whatever icons and messages you want to display.
The first thing to do is to load the required assembly System.Windows.Forms to gain access to the notification capabilities and then create the notification object.
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) $notification = New-Object System.Windows.Forms.NotifyIconWith this, you are then able to define what kind of title and message that your popup will display and show.
BalloonTipText :BalloonTipIcon : None
BalloonTipTitle :
ContextMenu :
ContextMenuStrip :
Icon :
Text :
Visible : False
Tag :
Site :
Container :
BalloonTipText is the message that you will show and BalloonTipTitle is, of course, the title that the balloon will display. You will also want to change the Visible property to True so the balloon will show up when you call it with the ShowBalloonTip method.
You will noticed that there is a property for an icon,not to be confused with the BalloonTipIcon property that shows the icon of the actual balloon pop-up. This dictates what icon will show up on the system tray when the notification is called. You can either supply your own icon or use one that is already available. In my case, I am using the Information icon via the SystemIcons namespace.
[System.Drawing.SystemIcons]::InformationListed below are the available icons from that namespace.
Application PropertyAsterisk Property
Error Property
Exclamation Property
Hand Property
Information Property
Question Property
Shield Property
Warning Property
WinLogo Property
When you look at the methods and properties using the Get-Member cmdlet, you will also see some events that can be used with some event monitoring to run specific actions based on if the balloon tip is closed or you wait for it to closed after the timeout or if you click on the balloon. There are also events that work with actions from the mouse but I will not be working with those.
Name MemberType—- ———-
BalloonTipClicked Event
BalloonTipClosed Event
BalloonTipShown Event
Click Event
Disposed Event
DoubleClick Event
MouseClick Event
MouseDoubleClick Event
MouseDown Event
MouseMove Event
MouseUp Event
CreateObjRef Method
Dispose Method
Equals Method
GetHashCode Method
GetLifetimeService Method
GetType Method
InitializeLifetimeService Method
ShowBalloonTip Method
ToString Method
Using the Register-ObjectEvent will allow you to leverage those events and have the perform specific actions based on how the event is handled. For instance, closing the balloon could allow the system tray icon to close or clicking on the balloon could allow another window to pop-up with information related to the pop-up. In my instance, I display messages that state whether the balloon was closed or clicked on.
The last thing you need to do is to call the notification which requires you to determine the timeout (measured in milliseconds) for how long the notification will remain in the system tray.
$notification.ShowBalloonTip(600)Putting everything together, I was able to generate a balloon pop-up that depending on whether you click it, close it or wait for it to close, a windows message will be generated that will tell you what action took place.
As you can see just by the examples, there is quite a bit you can do with the notifications and the actions that are associated with the notifications. Below is the code I wrote to perform the balloon pop-ups. Feel free to use and test with other things for reporting or configuration changes.
For more information regarding Balloon Notification alerts, check out this site:
http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx
#Load the required assemblies[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)#Remove any registered events related to notifications
Remove-Event BalloonClicked_event -ea SilentlyContinue
Unregister-Event -SourceIdentifier BalloonClicked_event -ea silentlycontinue
Remove-Event BalloonClosed_event -ea SilentlyContinue
Unregister-Event -SourceIdentifier BalloonClosed_event -ea silentlycontinue#Create the notification object
$notification = New-Object System.Windows.Forms.NotifyIcon
#Define the icon for the system tray
$notification.Icon = [System.Drawing.SystemIcons]::Information
#Display title of balloon window
$notification.BalloonTipTitle = “This is a Balloon Title”
#Type of balloon icon
$notification.BalloonTipIcon = “Info”
#Notification message
$title = “This is the message in the balloon tip.”
$notification.BalloonTipText = $title
#Make balloon tip visible when called
$notification.Visible = $True
## Register a click event with action to take based on event
#Balloon message clicked
register-objectevent $notification BalloonTipClicked BalloonClicked_event `
-Action {[System.Windows.Forms.MessageBox]::Show(“Balloon message clicked”,”Information”);$notification.Visible = $False} | Out-Null
#Balloon message closed
register-objectevent $notification BalloonTipClosed BalloonClosed_event `
-Action {[System.Windows.Forms.MessageBox]::Show(“Balloon message closed”,”Information”);$notification.Visible = $False} | Out-Null
#Call the balloon notification
$notification.ShowBalloonTip(600)
Hoping you will see this… I have a script that isn’t working… would you mind taking a look at this? [void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$objNotifyIcon.Icon = “icon.ico”
$objNotifyIcon.BalloonTipIcon = “None”
$objNotifyIcon.BalloonTipText = “Body text or description”
$objNotifyIcon.BalloonTipTitle = “Title”
$objNotifyIcon.Visible = $True
register-objectevent $objNotifyIcon BalloonTipClicked BalloonClicked_event -Action Start-Process -FilePath “C:\users\Public\ITCMD\Toggler\Toggle.bat”
$objNotifyIcon.ShowBalloonTip(10000)
$objNotifyIcon.dispose()
Giving me error messages and the icon does not show up. Thanks a lot!
I think it’s a cool script, but the issue when running on the command prompt, how is this solved?
I mean the question “Do you know why your script works fine in Powershell_ISE but when running in the console doesn’t seem to catch the click events?”.
Please let me know!
I’m curious, as I’m too much of a PS beginner, instead of showing a messagebox saying “balloon clicked” or something like that, can you instead make an IE window open up to a specific URL? I’m not sure how to use the -Action parameter you have there. Any help much appreciated
Actually I figured out that first question, but now I’m running into problem where the icon in the notification area never actually disappears. Regardless of whether I use your original script, or my modified version, whether I click the notification or not. Any ideas?
Add this in the action block:
$notification.dispose()
This will allow the icon to close.
Are these lines neccessary because I don’t see you ever creating “notification_event”?
## Clear any previous events
Remove-Event notification_event -ea SilentlyContinue
Do you know why your script works fine in Powershell_ISE but when running in the console doesn’t seem to catch the click events? I’ve tried starting the console with the -STA switch without luck. I’d love to get this working in order to have get two different behaviors depending upon the user’s input.
Nice catch. I think it was leftover from some testing I was doing with the code. I’ll remove it from my example as to not confuse anyone else. Thanks!
Not sure why it is not catching the click events when running via console. I just gave it a run and had no issues with it at all.