DEV Community

IT Command
IT Command

Posted on

4

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

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.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay