What is the PowerShell command equivalent to selecting "Sleep" from the Win7 menu?

1

What PS command(s) do I use to put my lappy to sleep like the description when I hover the mouse over the Windows menu "Sleep" option:

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

These SE SuperUser threads have some ideas about similar solutions:

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

How to script the Windows 7 'sleep' command

...but it apparently the rundll32.exe corrupts the stack and these solutions are hibernating, not "sleeping".

When "Sleep" is selected from the menu, I am assuming there's no magic invoked that can't be replicated with a simple command or an alias to a script?

=========================================

per user @jnL (much obliged!) I now have the following in my PS profile and can invoke "Sleep" with nap: 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

Mr. Kennedy

Posted 2016-08-23T06:20:15.757

Reputation: 635

getting warmer: http://stackoverflow.com/questions/20713782/suspend-or-hibernate-from-powershell

– Mr. Kennedy – 2016-08-23T06:37:47.517

See also https://superuser.com/a/395497/111303

– Vladimir Reshetnikov – 2018-06-29T19:44:13.510

Answers

5

With your provided link in the comment section...

Create a PS script or run following lines directly in PowerShell:

# load assembly System.Windows.Forms which will be used
Add-Type -AssemblyName System.Windows.Forms

# set powerstate to suspend (sleep mode)
$PowerState = [System.Windows.Forms.PowerState]::Suspend;

# do not force putting Windows to sleep
$Force = $false;

# so you can wake up your computer from sleep
$DisableWake = $false;

# do it! Set computer to sleep
[System.Windows.Forms.Application]::SetSuspendState($PowerState, $Force, $DisableWake);

jnL

Posted 2016-08-23T06:20:15.757

Reputation: 308

swEEt - I've put these commands/parameters in a function in my PS profile and can now invoke 'Sleep' with the word "nap"! http://superuser.com/questions/505247/how-to-script-the-windows-7-sleep-command/1116924#1116924

– Mr. Kennedy – 2016-08-23T23:57:05.580

1Glad I could help :) – jnL – 2016-08-24T06:35:49.927