A Script to put Windows 7 into sleep (stand by)

Arman KhodabandeFreelance Developer
Always looking for challenges and problems to solve !
This has made me more and more strong . . .
In my daily work with my laptop I really felt that I needed a script to quickly put the laptop into Sleep (Stand by, Suspend to RAM) with one key stroke (using the top media keys).
Searching the web, many people suggest to use the following command:
 rundll32.exe powrprof.dll,SetSuspendState 

Open in new window

But it didn't work for me because it hibernates the machine instead of Sleep, which is the case if you have hibernate enabled - as most of us have.

So I gathered my VBS knowledge and my old scripts to come up with a VB script to MAKE the machine to go to sleep.
Here's the VBS script:

Dim objShell
                      Set objShell = CreateObject("Shell.Application")
                      set objShell = nothing
                      Set WshShell = CreateObject("WScript.Shell")
                      With objShell
                      WScript.Sleep 200
                      WshShell.AppActivate "Shut Down Windows"
                      WshShell.SendKeys "ss"
                      WshShell.SendKeys "{TAB}"
                      WshShell.SendKeys "{Enter}"
                      End With
                      Set objShell=Nothing

Open in new window

You should copy and paste the code into notepad (for example) and save it as a VBS file.
Then double click the vbs to sleep.

Note>> If the script doesn't work:
If your computer is a low performing machine or the response rate for windows is low (windows come up with a delay) due to high CPU usage most of the time, the script may not run properly and only brings up a shutdown window. To solve this issue:

1. Open your VBS file in a text editor like notepad or wordpad.
2. Go to 7th line and find "Sleep 200"
3. That's a pause time to let the windows brings up the shutdown dialog. You can increase the wait time by increasing 200 to 500 (for example). Note that this is the wait time in milliseconds so if you go over 1000 you get more than 1 second delay and the script may not seem good at all :D

For those who need a bat file for this, use the following code and save it as BAT:

@ echo off
                       > %temp%\Sleep.vbs echo Dim objShell
                      >> %temp%\Sleep.vbs echo Set objShell = CreateObject("Shell.Application")
                      >> %temp%\Sleep.vbs echo objShell.ShutdownWindows 
                      >> %temp%\Sleep.vbs echo set objShell = nothing 
                      >> %temp%\Sleep.vbs echo Set WshShell = CreateObject("WScript.Shell") 
                      >> %temp%\Sleep.vbs echo With objShell 
                      >> %temp%\Sleep.vbs echo WScript.Sleep 200 
                      >> %temp%\Sleep.vbs echo WshShell.AppActivate "Shut Down Windows" 
                      >> %temp%\Sleep.vbs echo WshShell.SendKeys "ss" 
                      >> %temp%\Sleep.vbs echo WshShell.SendKeys "{TAB}" 
                      >> %temp%\Sleep.vbs echo WshShell.SendKeys "{Enter}" 
                      >> %temp%\Sleep.vbs echo End With 
                      >> %temp%\Sleep.vbs echo Set objShell=Nothing
                      wscript.exe %temp%\Sleep.vbs
                      del %temp%\Sleep.vbs

Open in new window

Then you can put this bat or vbs files on your desktop or if you have a laptop assign the top Media keys to run this files when pressed...
I myself compiled the bat file to a standalone exe and assigned my Dell touch media keys to run the EXE file and I can now sleep with one touch!!

Enjoy and Have a good sleep :D
Arman KhodabandeFreelance Developer
Always looking for challenges and problems to solve !
This has made me more and more strong . . .

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.