How to script the Windows 7 'sleep' command

3

To clarify, when I'm talking about 'Sleep' I mean Hybrid Sleep, i.e. save the contents of RAM to the hibernation file and then go in to Standby mode.

I have this working perfectly fine via the start menu, but want to script it in the form of something that can be run by the Task Scheduler. An exe, batch script or Powershell script would work well

I've experimented with PowrProf SetSuspendState and powercfg -h off in scripts with no luck, it always just goes straight into Standby or hibernates and turns off entirely

benwh

Posted 2012-11-13T22:00:58.333

Reputation: 379

Answers

3

I figured out my solution from this VERY helpful answer by user @jnL (please upvote his contribution!) It works on my win7 HP lappy and makes it "sleep" in the same way as if I selected Sleep from the Windows Start Menu GUI, i.e.

Keeps your session in memory and puts the computer in
a low-power state so that you can quickly resume
working.

Now that I've added this script to my PowerShell profile C:\Users\user_name\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

function sleepy_time {
Add-Type -AssemblyName System.Windows.Forms
$PowerState = [System.Windows.Forms.PowerState]::Suspend;
$Force = $false;
$DisableWake = $false;
[System.Windows.Forms.Application]::SetSuspendState($PowerState, $Force, $DisableWake);
}

new-alias -name nap -value sleepy_time

...when I enter nap at the PowerShell prompt my laptop goes to sleep, fan spins down and then when I move the mouse or press spacebar (etc...) it wakes up to a login screen.

Note that the rundll32.exe solution apparently corrupts the stack, per a comment in this thread by user @accolade referencing this answer: https://superuser.com/a/331545/21887

Mr. Kennedy

Posted 2012-11-13T22:00:58.333

Reputation: 635

4

I found an article titled Command line hybrid sleep that says you may be able to use the following command:

rundll32 powrprof.dll,SetSuspendState

From the comments there, it sounds like you'll also need make sure Hybrid Sleep mode enabled in the Power Configuration options and perhaps Activate Hybrid Sleep in the BIOS as well, depending on your system.

martineau

Posted 2012-11-13T22:00:58.333

Reputation: 3 849

/h hibernates the machine, it doesn't sleep it. – Enigma – 2013-02-04T16:58:26.707

@Enigma: You're correct. I've updated my answer. – martineau – 2013-02-04T21:08:46.663

As stated in the question, I've been unable to get this to work via the Task Scheduler – benwh – 2013-03-16T18:19:56.867

benwh: Yes, I can read. An important point I was trying to make my answer was that in order to get it to work with the Task Scheduler, may require enabling it in a one or two places. – martineau – 2013-03-16T19:36:58.927

If it wasn't configured correctly elsewhere in the system, why would Hybrid Sleep work correctly when initiating it via Start -> Sleep, or using that command via the run prompt? – benwh – 2013-03-16T21:27:21.780

benwh: The first link is to a discussion where rundll32 command does not put the system into hybrid sleep, even though hybrid sleep is enabled and can be entered into manually via the Start menu -- because of a mismatch with the ACPI settings in the BIOS, I believe. – martineau – 2013-03-17T00:52:20.373

You haven't mentioned whether you using a desktop or laptop, but apparently hybrid sleep is off by default on laptops and you may need to turn it on.

– martineau – 2013-03-17T01:45:26.347

1

Using the script mentioned in an earlier answer, I've rewrote it to improve it's usability. Save it as a powershell module (.psm1) to the system profile folder ($PsHome\Profile.ps1).

Then create a schedule task to run at the time you want to place the computer to sleep. In the actions tab set to the parameters you want.

Example:

Powershell -ExecutionPolicy Bypass -NoLogo -NonInteractive -Command "& {nap -PowerState Hibernate}"

function Suspend_Computer {
<#
.Synopsis
    Sets the computer to a specified power mode.
.Description
    The computer will be placed either in sleep or hibernation modes.
.Example
    C:\PS>Suspend_Computer
    Description
    -----------
    The command will run using default parameters which are not force the action of Suspend.
.Example
    C:\PS>Suspend_Computer -Force $True -PowerState Hibernate
    Description
    -----------
    The computer will save all data to disk and hibernate.
.Link
    https://superuser.com/questions/505247/how-to-script-the-windows-7-sleep-command
#>
Param(
[Parameter(Mandatory =$False,Position=0)]
$Force = $False,

[Parameter(Mandatory=$False,Position=1)]
$DisableWake = $False,

[Parameter(Mandatory=$False,Position=2)]
[ValidateSet("Suspend","Hibernate")]
[string]$PowerState = 'Suspend'
)
    Add-Type -AssemblyName System.Windows.Forms
    $PowerState = [System.Windows.Forms.PowerState]::$PowerState;
    [System.Windows.Forms.Application]::SetSuspendState($PowerState, $Force, $DisableWake);
}

new-alias -name nap -value Suspend_Computer

Jessie

Posted 2012-11-13T22:00:58.333

Reputation: 66