Sleeping a computer via batch file in Windows 7

14

10

I want to make a batch file that will sleep my computer after a period of time.

The closest thing I have found is placing it in standby, but seems to work differently than the sleep button on my keyboard (longer bootup, doesn't wake on mouse click). Any ideas? Would it be easier in Python or C#?

The less then ideal technique mentioned above is calling this in the cmd prompt:

Rundll32.exe powrprof.dll,SetSuspendState

Saebin

Posted 2010-05-26T06:16:33.693

Reputation: 211

Just add a countdown part into the .bat and execute this after that. – Apache – 2010-05-26T07:30:51.787

looks like duplicate of http://superuser.com/questions/83437/hibernate-computer-from-command-line-on-windows-7

– akira – 2010-05-26T07:33:02.283

Hibernation is totally diff, but yeah thats the correct solution I guess. – Apache – 2010-05-26T07:36:07.253

I believe the rundll thing is the same as hibernation. With sleep, you click the mouse and your login screen is there in a couple seconds... hibernation has to recheck the bios, harddrives, and transfer the saved state from the harddrive to ram and then boot windows. The only drawback to sleep is if you lose power and the saved state stored in ram is lost (and older pcs actually leave more components on like PSU and fans). – Saebin – 2010-05-26T07:43:42.530

Answers

26

Simply command

rundll32.exe powrprof.dll,SetSuspendState

Does system hibernate, but if you write this:

rundll32.exe powrprof.dll,SetSuspendState 0,1,0

And turn off hibernation with command:

powercfg -hibernate off

Now your system will go asleep, and will wake up on mouse move if selected a device for it (scroll down to learn how).

Batch a timer:

timeout /t 1200
rundll32.exe powrprof.dll,SetSuspendState 0,1,0

Where 1200 is seconds, which mean after 20 minutes batch will run sleep command.

Optionally you can prevent canceling countdown (if you press any key in batch window):

timeout /t 1200 /nobreak
rundll32.exe powrprof.dll,SetSuspendState 0,1,0

Little HOWTO about waking up with mouse:

Go to: Start -> Control Panel -> Mouse.

alt text

In the Mouse Properties window, click on the Hardware tab and select your mouse from the list of devices. Normally, there will only be one mouse listed here but that will depend on the hardware you have connected to your computer. When you have selected your mouse from the list, click the Properties button.

alt text

Now in the Properties window for your mouse, click on the Change Settings button on the General tab.

alt text

In the window that opens, click the Power Management tab and check the option titled Allow This Device to Wake The Computer. Click the OK button on this window and click the OK button on the Mouse Properties windows that is still open. From now on, you can wake up Windows 7 from sleep mode by clicking a mouse button or moving the mouse around.

alt text

Anotomix

Posted 2010-05-26T06:16:33.693

Reputation: 657

3wow man, you really like answering questions :) – Omu – 2010-05-26T10:50:54.263

2

Working Solution!
I had to use the PsShutdown utility to allow proper sleeping (primary issue is SetSuspendState does not allow wake timers to wake up the machine). My batch file to sleep is 1 simple line:

PsShutdown -d -t 2

P Pace

Posted 2010-05-26T06:16:33.693

Reputation: 21

Please read How do I recommend software for some tips as to how you should go about recommending software. You should provide at least a link, some additional information about the software itself, and how it can be used to solve the problem in the question.

– DavidPostill – 2016-07-26T08:10:22.013

1This didn't work in my pc... – mrbengi – 2017-07-09T08:42:15.453

2

shutdown /h

will hibernate the computer (not sure about suspend though).

Stacey Richards

Posted 2010-05-26T06:16:33.693

Reputation: 1 292

1

.NET to the rescue. This solution put computer to sleep without need of changing PC config (turning off hibernation). Run this command in batch:

powershell.exe -command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.Application]::SetSuspendState([System.Windows.Forms.PowerState]::Suspend, $false, $false)"

You can use either ::Suspend for sleep, or ::Hibernate (which the way the other answers work). Documentation for SetSuspendState.

As System.Windows.Forms namespace is not loaded by default (it does in Powershell ISE), you need to load assembly for that at first.

original answer

Velda

Posted 2010-05-26T06:16:33.693

Reputation: 1 090

0

Just create a batch file and insert the following lines:

7200 = 2 hours

@echo off
timeout /t 7200 /nobreak
powercfg -h off
rundll32.exe powrprof.dll,SetSuspendState 0,1,0
powercfg -h on

Chris Fischer

Posted 2010-05-26T06:16:33.693

Reputation: 1

0

You may try SetSystemPowerState to avoid having to disable Hiberanate.

rundll32.exe kernel32.dll,SetSystemPowerState

Note that adding 1,0 or 1 0 or even doing Rundll32.exe powrprof.dll,SetSuspendState 0 0 1 or Rundll32.exe powrprof.dll,SetSuspendState 0,0,1 that the arguments (0,0,1) mean NOTHING.

The powrprof.dll,SetSuspendState does not parse rundll32 style arguments and thus ignores them. That is why you have to disable hibernate.

Rodney

Posted 2010-05-26T06:16:33.693

Reputation: 11

1This is useful for Windows XP, not Vista or later. – Rodney – 2013-09-27T21:35:26.270

1Note that rundll32 ignores the arguments you give it (hence why SetSuspendState always hibernates) since the DLL isn't designed to accept arguments from rundll32. – Rodney – 2013-09-27T21:37:26.720

-1: This is outright wrong. It doesn't work under the OP's desired operating system. – nc4pk – 2013-09-27T22:28:15.133

rundll32 is NOT passing the arguments hence why you have to disable Hibernate to make it work. The default args tell it to always hibernate, but if hibernate is disabled the DLL API defaults to sleep. – Rodney – 2013-11-08T16:54:07.730

0

This question has already been answered.

powercfg -h off & start /min "" C:\WINDOWS\System32\rundll32.exe PowrProf.dll,SetSuspendState 0,1,0 & ping -n 3 127.0.0.1 > nul & powercfg -h on & exit

See the following:

What is the command to use to put your computer to sleep (not hibernate)?

Josh

Posted 2010-05-26T06:16:33.693

Reputation: 348

1If you believe this question is a duplicate, feel free to flag it as such. – Moses – 2013-10-09T01:37:06.837