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.

  • +1 Good question. I'm aware there are specific scripts you can add to in Ubuntu Linux for this (though I'd like to know specifically which myself). Anyway, I've totally had this come up in using Windows too. Aug 9, 2011 at 23:36

7 Answers 7


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.
  • 2
    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. Nov 26, 2015 at 9:12
  • When I search for event ID 42 in the log, I only see the events for when the system enters hibernate "Sleep Reason: Hibernate from Sleep". Can't find any events for the initial sleep ("modern standby" or whatever it's called in win11).
    – Dan M.
    Nov 21, 2023 at 15:13

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.

  • Bingo. I have a task scheduled that runs SyncToyCmd.exe -R daily, so it should work fine with the other triggers, too. Aug 13, 2011 at 4:59

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:

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

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

  • You've been very helpful. I have tried many variants with EventID=42 on Windows 10 and 11. Only with XML code and manual settings was the problem solved. Thank you.
    – DigiBat
    Aug 28, 2023 at 16:14

What you can do is create a batch file to do everything you need to before shutting down or hibernating the machine.

Mind you you should run the apps with "Start / wait xxx.exe" so the batch file will wait for the command to complete and exit and not reach the shutdown part before other tasks are complete.

The last command in that file should be "shutdown xxx yyy " where xxx and yyy are the parameters for the shutdown command. Then you can just run this batch file instead of using any other shutting down command / application.


There is an AutoHotkey script that attempts to solve this here


Having tried some of the prescribed methods in here, I was never able to get a script to run on sleep. The built-in Windows Task Scheduler facilities just didn't fix it.

I found a small open source utility called "Power Triggers" or "Windows 7 Suspend/Resume Control". Despite not being well-polished or currently maintained, I found this tool to work on Windows 10 without issues. However, to run a script easily on suspend or resume, you have to use version 1.01 (aka release 1 in the archive), and not the latest version.

  • 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? Aug 1, 2021 at 4:03

Install lib of python.

`pip install pyautogui`

Run script after 180 second.

import pyautogui
import time

while True:
# Get the current coordinates of the mouse cursor
   current_x, current_y = pyautogui.position()

# Display the current mouse coordinates
   print(f"Current mouse position: ({current_x}, {current_y})")

# Automatically click at the current position of the mouse cursor
   pyautogui.click(current_x, current_y)

# Display a message after the click operation
   print("Auto click completed.")

# Wait for 3 minutes before repeating the process
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jan 31 at 7:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .