Win7: Automatic scripts before and after hibernation

hibernatewindows 7

How can I scripts automatically before hibernation and after waking up form hibernation? I am searching for something similar to the Logon/logoff and startup/shutdown scripts feature.

Best Answer

There's nothing built into the OS, but you can get a dev to code something for you...you need to capture the WM_POWERBROADCAST event that fires when the system changes state.

I found a sample script that works with AutoHotKey:

OnMessage(0x218, "WM_POWERBROADCAST") 
Return 

WM_POWERBROADCAST(wParam, lParam) 
{ 
   ; PBT_APMQUERYSUSPEND = 0 
   ; PBT_APMRESUMESUSPEND = 7 
   FileAppend, "powerbroadcast", Z:\Temp\pokus.txt 
   If (wParam == 0) { 
      FileAppend, "suspend", Z:\Temp\pokus.txt 
      Return true 
   } Else If (wParam = 7) { 
      FileAppend, "resume", Z:\Temp\pokus.txt

You could replace "FileAppend" with another AutoHotKey command.

You could make something up in Powershell probably but until Powershell 2.0 is released the "run in the background" capability isn't the best. There is a Powershell example in this thread in any case.