I would like to run a script in Windows right before standby/sleep mode with admin rights and blocking, i.e., the system should not go to sleep until the script has successfully terminated.

Alternatively, it would be possible for me to create a background process or service if it can be done in a simple manner.

Background: It seems I have a very buggy driver for a device which leaves Windows hanging with a blank screen when going to standby or presents a blank screen upon wakeup. This is not 100% reproduceable, it happens only sometimes. However, it seems when I just disable the corresponding device in the device manager, everything works fine. I would like to automate this process.


I would like to run a script in Windows right before standby/sleep mode

You can use the Task Scheduler to create a task that is triggered by a Kernel-Power log event that indicates "Sleep":

  1. Run "Task Scheduler".
  2. Select "Task Scheduler Library
  3. Click menu "Action" > "Create Task"
  4. Select "General" and give the task a "Name" and Description"
  5. Select "Triggers" and click "New"

    • Set "Begin the Task" to "On an Event"
    • Set "Log" to "System"
    • Set "Source" to "Kernel-Power"
    • Set "Event ID" to "42" (Sleep)
    • Click "Ok"

    enter image description here

  6. Select "Actions" and click "New"

    • Set "Program/script" to a script of your choice
    • Click "Ok"

    enter image description here

When I disable the corresponding device in the device manager everything works

I would like to automate this process.

Use devcon to disable the device.


  • Use devcon find to get the device hardware ID.
  • Use devcon disable HardwareID to disable the device.
  • Add devcon disable HardwareID to the script created above.
  • On Windows 7 64 bit you need to use the 32 bit version of devcon, as the 64 bit version gives the following error:

    This version of F:\test\devcon\ia64\devcon.exe is not compatible with the version of Windows you're running. Check your computer's system information to see whether you need a x86 (32-bit) or x64 (64- bit) version of the program, and then contact the software publisher.

  • The download link mentioned below is a zip file containing both 32 and 64 bit versions of devcon.

Where do I download devcon?

Go to the http://support.microsoft.com page The DevCon command-line utility functions as an alternative to Device Manager for the download link.

Further Reading

  • Thanks, great pointer but this only works for shutdown. I would need it for standby and sleep mode. – divB Aug 22 '15 at 18:05
  • 1
    @divB Answer updated to use Task Scheduler to run a script on the appropriate Kernel-Power event (EventID 42 is Sleep). – DavidPostill Aug 22 '15 at 19:46
  • @divb Note standby and mode are the same thing. See What is the difference between Sleep and Standby modes? – DavidPostill Aug 22 '15 at 19:51
  • This works now for standby which is great but it is not blocking: For a test, I just let execute calc.exe. When I switch to standby, calc.exe pops up potentially long after I resumed again! Since the buggy driver leaves the system hanging when it goes to standby, I need to disable it before it actually goes into standby. – divB Aug 23 '15 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.