'Need to run a powershell script every time system wakes up from Sleep

I just finished writing a script with PowerShell and I need it to run every time my system wakes up from sleep or Hibernation. I tried the gpedit.msc -> User Config -> Windows Settings -> Scripts -> Logon but this does not work when my system Wakes up from 'Sleep'... probably because 'Sleep' - > 'Wake -up' is not technically new logon (As a user I am already logged-on).

Either a Sleep-Wake event or Login (User Login) event should trigger my script.

Any ideas on how I can do this?

More Info on what my script does: It checks if internet/network is connected. If yes, it will attempt to run the Cisco VPN connection command line utility with required arguments and logs me into VPN. Every time I wake my system up from sleep and login I need the damn VPN to connect me automatically.



Solution 1:[1]

You can accomplish this by scheduling a task to run the script, triggering on event "Power-Troubleshooter" with Event ID 1. This is the event that's logged by the system when it resumes from sleep. When the system is entering sleep, the "Kernel-Power" event is called with Event ID 42 (unlike what another answer here suggests).

Hope that helps :)

Solution 2:[2]

Task Scheduler would seem to be a better fit.

If a task trigger of "At Startup" doesn't work in this case (I suspect it won't) then identify an event log entry posted on wake from sleep and trigger on that.

Solution 3:[3]

On Windows 11 the Event Id we can use for this is 566

I set the Trigger as> On an Event -> System -> Kernel-Power -> 566

Here is my configuration Scheduled task triggers

In my case I run a powershell script that turns on my keyboard backlight on my laptop. The backlight now turns on after windows has resumed from sleep (open the lid on laptop) but before I have logged in.

You also might want to uncheck: conditions > Power > "start the task only if the computer is on AC power"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ratnesh Chandna
Solution 2 Richard
Solution 3 Community