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
  • 556

3 Answers3

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
  • 4,573
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

MmmHmm
  • 768
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
  • 166