How to execute a script on sleep, hibernate, resume, and shutdown

30,611

Solution 1

Try this when the computer returns from Hibernate or Sleep Mode

Begin the Task: On an event
Setting Basic
Log:  System
Source: Power-Troubleshooter
Event ID: 1
The system has resumed from sleep.

When you put the computer in Hibernate or Sleep Mode

Begin the Task: On an event
Setting Basic
Log:  System
Source: Kernel-Power
Event ID: 42
The system is entering sleep.

Solution 2

You can create a task that uses any of the following options as triggers for the Task Scheduler in Windows 7:

  • On Schedule
  • On Logon
  • At Startup
  • On Idle
  • On An Event
  • On connection to User Session
  • On disconnect from User Session
  • On Workstation lock
  • On Workstation unlock

There's lots to work with there, I'd be looking at the on Idle, on Workstation lock and On Workstation Unlock triggers. That's probably your best bet.

Hope that helps.

Solution 3

When I wanted it set up, I wanted a very low-latency approach so I could get a splash screen going. For task scheduler you want to set the trigger to "on an event" then set to custom rather than basic, then edit the query manually under event trigger's XML tab.

I tried this script for XML:

<QueryList>
  <Query Id="0" Path="System">
    <Select Path="System">*[System[Provider[@Name='Microsoft-Windows-Kernel-Power'] and (Level=4 or Level=0) and (EventID=42)]]</Select>
  </Query>
</QueryList>

If you need the application to start up in a snap, this is the best way to do it.

Share:
30,611

Related videos on Youtube

Jason
Author by

Jason

Updated on September 18, 2022

Comments

  • Jason
    Jason almost 2 years

    I keep alot of my classwork documents hosted on dropbox as well as my personal site for remote storage. This helps alot since I switch between an iPad, laptop and desktop, so there are no worries about my documents being out of touch.

    Problem is, this solution relies on me remembering to execute the sync program (synctoy) for win7, so I'd like a solution to do this automatically. I've found some scripting help that should work on bootup, but how can I get the script to execute on entering sleep and hibernate, resuming from both, as well as executing before shutdown.

  • Andrew Coleson
    Andrew Coleson almost 13 years
    Bingo. I have a task scheduled that runs SyncToyCmd.exe -R daily, so it should work fine with the other triggers, too.
  • Andreas Haferburg
    Andreas Haferburg over 8 years
    Doesn't work when I Hibernate. I can see the event getting logged in Event Viewer, but the script that I specified in Task Scheduler is only running after the system returns from Hibernate. According to the Task Scheduler history, the task is triggered but not run.
  • shadowz1337
    shadowz1337 almost 3 years
    Could you please share the compiled version? I built the source code using Visual Studio, I can run the exe, but it is throwing some error messages when I try to attach a .bat file to it. Also, could you please confirm if that tool is able to run the script BEFORE hibernation/sleep events?