DEV Community

IT Command
IT Command

Posted on

How do I Include Click Event in Powershell Notification?

Here's What I've Got

-A powershell script that can create a notification. This working script can be found Here
-A Batch script to call said script

Here's What I need

-For my powershell script to also start a program when it is clicked on
-That's it
I know it's possible. I don't know how. Note that I'm using Windows 10.
Here's an updated version of what I have so far:

 [void]         

 $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) 

Note that what I have right now does not let me start a program, and also leaves the icon in the system ^ tray, and it cannot be removed without restart.(big problem).

Top comments (1)

Collapse
 
4lch4 profile image
Devin W. Leaman

This mostly comes down to how you're executing the PowerShell script itself. If you're running it within the ISE, it'll show the notification but the script shuts down and isn't available to catch the click event. I wrote a small script that might help point you in the right direction.

In short, I create a function that shows the notification, dot-source the script that has the function, then call the function from the console or another file as needed and it does what's expected.