<

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

Published on
16,576 Points
10,276 Views
3 Endorsements
Last Modified:
Approved
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")
objShell.ShutdownWindows
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
Kpax7
3
Comment
0 Comments

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Join & Write a Comment

This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month