Saturday 23 November 2019

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


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

Answer



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);

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...