1

I want to run exe file when the computer wakes up from the sleep mode, How can I do that in windows 10?

1
  • Here's how I would figure this out for a starting point... 1. find a correlated event viewer event that occur for waking up from sleep, 2. and then create a scheduled task to execute the executable file with that event id, etc. being the trigger. Again, a simple starting point to dig into some more. Commented Aug 3, 2019 at 2:45

1 Answer 1

0

The way you could achieve this, is with a visual basic script which will loop and check the time.

If the time, your PC was in sleep or standby mode, would be greater then 20 seconds ( set in the code below ), then the script will execute the code between Rem A and Rem B.

If you'd put it in the startup folder, it'll be executed every time you start your Computer.

You have to click (or doubleclick) it once so it is running for when you put your PC in sleep or in standby mode. You can put your own code between REM A and REM B.

Function GetDiff(Tm_a, Tm_b)
If (isDate(Tm_a) And IsDate(Tm_b)) = False Then
GetDiff = "00:00:00"
Exit Function
End If
seconds = Abs(DateDiff("S", Tm_a, Tm_b))
minutes = seconds \ 60
hours = minutes \ 60
minutes = minutes Mod 60
seconds = seconds Mod 60
If Len(hours) = 1 Then hours = "0" & hours
GetDiff = hours & ":" & Right("00" & minutes, 2) & ":" & Right("00" & seconds, 2)
End Function
Do
a = Now
WScript.Sleep 5000
b = Now

If GetDiff(a, b) > "00:00:20" Then

Rem A

Set ob = CreateObject("Wscript.Shell")
UsrNm = ob.expandenvironmentstrings("%Username%")
UsrPrfl = ob.expandenvironmentstrings("%Userprofile%")
MsgBox "Hi " & UsrNm & "  ( " & UsrPrfl & " )" & vbCrLf & _
        "You just came out of Hibernate or Standby mode." & vbCrLf & _
        "Instead of using an MsgBox, you can execute a program, command or code here," & vbCrLf & _
        "e.g.  ( ob.Run " & Chr(34) & Chr(34) & Chr(34) & "C:\Program Files\Internet Explorer" & Chr(34) & Chr(34) & Chr(34) & " )" _
        & vbCrLf & vbCrLf & _
        "Notice the three quotation marks in front and after the path," & vbCrLf & _
        "They are required when you open a custom folder or file," & vbCrLf & _
        "like  .exe  .jpg  .vbs ...etc," & vbCrLf & _
        "otherwise you will get the error ( The system can not find the file specified )", 4096, "Your pc nap time : " & GetDiff(a, b)

Rem B

End If
Loop

Paste the above code in a text file, give it a proper name, and change the extension to .vbs

If you can't see the extension, open Run ( win key + R ) Paste the below string in the Run text box :

RunDll32.exe shell32.dll,Options_RunDLL 7

and hit Enter. This will open Folder Options in the View tab.

Deselect Hide Extensions for Known File Types and you'll be able to see the extensions.

Then place the .vbs file in the startup folder so it'll be executed every time you start Windows.

Open the startup folder :

Paste the string below into Run ( win key + R ) paste the string and then press Enter, or create a shortcut with it :

"%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

I'm using this script for a long time now, and it has never failed me.

If you want to execute your executable ( or something else ) when coming out of sleep or standby mode, erase everything between Rem A and Rem B and use the folowing code :

Set ob = CreateObject("Wscript.Shell")

ob.Run """Paste the entire path of your executable between these quotes"""

e.g. ob.Run """C:\Program Files\Internet Explorer\iexplore.exe"""

Be sure to have only three quotes in front and behind the path. You have to click the script one time, and then it will loop infinitely even when the PC comes out of sleep or standby mode.

If you have further questions, please ask.

Significant information :

The set task ( between REM A and REM B ) will be executed before you have entered your Password, provided that you have one, and have set "Require a Password on Wakeup" in powercfg.cpl ( better known as Power Options )

You must log in to answer this question.

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