<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

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

Published on
17,051 Points
10,751 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

Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Join & Write a Comment

In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month