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
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);
  • swEEt - I've put these commands/parameters in a function in my PS profile and can now invoke 'Sleep' with the word "nap"! superuser.com/questions/505247/… – Mr. Kennedy Aug 23 '16 at 23:57
  • 1
    Glad I could help :) – jnL Aug 24 '16 at 6:35

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.