I know that almost everything on Windows, like opening any sort of application, can be done from the command prompt or from the Run menu.
How can I put my computer to sleep or shut it down? What is the command for that?
I know that almost everything on Windows, like opening any sort of application, can be done from the command prompt or from the Run menu.
How can I put my computer to sleep or shut it down? What is the command for that?
You will find shutdown.exe to be your friend.
Other handy commands see this post:
Sleep Computer (read more at https://superuser.com/a/463652/249349 )
Lock Workstation
Hibernate Computer - see answers by Scott Chamberlain and Eric L.
Restart Computer
Shutdown.exe -r -t 00
Shutdown Computer
Shutdown.exe -s -t 00
EDIT/UPDATE:
It seems that sleeping a computer is problematic if hibernate is turned on.
Copying from other answers:
You can either try PsShutdown or:
The command rundll32.exe powrprof.dll,SetSuspendState 0,1,0 for sleep is correct - however, it will hibernate instead of sleep if you don't turn the hibernation off.
Here's how to do that:
Go to the Start Menu and open an elevated Command Prompt by typing cmd.exe, right clicking and choosing Run as administrator. Type the following command:
powercfg -hibernate off
The methods posted by other people do not work correctly if a computer has hibernation enabled, the computer will not wake on Keyboard or, more importantly, not wake on scheduled task.
One of Microsoft's Sysinternals tool is PsShutdown using the command psshutdown -d -t 0 it will correctly sleep, not hibernate, a computer
The command rundll32.exe powrprof.dll,SetSuspendState 0,1,0 for sleep is correct - however, it will hibernate instead of sleep if you don't turn the hibernation off.
Here's how to do that:
Go to the Start Menu and open an elevated Command Prompt by typing cmd.exe, right clicking and choosing Run as administrator. Type the following command:
powercfg -hibernate off
NirCmd workes on Windows 8 (I assume it also works with Windows 7) → http://www.nirsoft.net/utils/nircmd.html
The command is standby, but it puts the computer to sleep.
nircmd.exe standby
If you have hybrid sleep enabled, it will work; i.e., it puts the computer to sleep and copies the memory to disk in case of power loss.
The shutdown part of the question is clear for everybody.
shutdown.exe /? shows all choices to shutdown / restart / advanced options boot / firmware boot.
But I see so much bashing thrown around about the Sleep part.
Everybody gets it that rundll32.exe should not be used to call random functions and it just works here for hibernate entirely by coincidence. But that's the nature of Windows - a black box that people have poked around and found hundreds of workarounds to get a job done (shout-out to dostips).
There are so many "proper" solutions proposing external tools, yet I can't seem to find a native (hybrid / pinvoke / whatever) batch script, even if this question has remained active over the years.
So here's my simple power_sleep.bat:
@echo off &mode 32,2 &color cf &title Power Sleep
set "s1=$m='[DllImport ("Powrprof.dll", SetLastError = true)]"
set "s2=static extern bool SetSuspendState(bool hibernate, bool forceCritical, bool disableWakeEvent);"
set "s3=public static void PowerSleep(){ SetSuspendState(false, false, false); }';"
set "s4=add-type -name Import -member $m -namespace Dll; [Dll.Import]::PowerSleep();"
set "ps_powersleep=%s1%%s2%%s3%%s4%"
call powershell.exe -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -Command "%ps_powersleep:"=\"%"
exit
It even works as a big one-line command:
powershell.exe -C "$m='[DllImport(\"Powrprof.dll\",SetLastError=true)]static extern bool SetSuspendState(bool hibernate,bool forceCritical,bool disableWakeEvent);public static void PowerSleep(){SetSuspendState(false,false,false); }';add-type -name Import -member $m -namespace Dll; [Dll.Import]::PowerSleep();"
You can initiate the suspend or hibernate options from the command line as specified below and referenced in this article.
%windir%\System32\rundll32.exe powrprof.dll,SetSuspendState
If your computer is set to allow hibernation then the above command will initiate hibernation. If the hibernation feature is disabled, then it will enter the standby state. As other users have answered previously, shutdown.exe /? will provide many shutdown options.
If you have Python (with PyWin32) available, you can also call that SetSuspendState function directly with:
import ctypes
ctypes.windll.PowrProf.SetSuspendState(0, 1, 0)
This can easily be put into a batch file or a shortcut. Sort of like:
pythonw -c "import ctypes; ctypes.windll.PowrProf.SetSuspendState(0, 1, 0)"
So then if you don't have your .py files associated to the interpreter, you can just double-click the link.
I have created a shortcut for rundll32.exe powrprof.dll,SetSuspendState 0,1,0 (also tried with 0,0,0), but running the shortcut seemed to put my PC into hibernation. I could not wake up the PC using the keyboard; I had to press the power button, and the PC showed the mainboard post messages, etc.
After reading the Windows API document, I created a very simple programme with just 3 lines of typing. I have uploaded the executable I compiled to this page (click the "SleepTest.exe"), but this file could be deleted after a while (this is a free file hosting site that I just found by a quick Google search).
If you do not trust me (which is totally fine) or the file has been deleted, you can compile the code yourself, Of course. You need to add "PowrProf.lib" to the additional dependencies of the Linker.
#include "stdafx.h"
#include "windows.h" <-- Added this to make it work on Windows.
#include "PowrProf.h" <-- Added this to use the sleep function.
int main()
{
SetSuspendState(FALSE, FALSE, FALSE); <-- Added this actual call.
return 0;
}
rundll32.exe powrprof.dll,SetSuspendState 0,1,0 seems to be doing the same thing, but somehow, the programme above did not put the computer into hibernation. I could wake up the PC instantly (no mainboard post messages, etc) by pressing any key on the keyboard.
You must first disable Hibernate in windows and then put the computer in Sleep state. Use following two commands:
powercfg -hibernate off
rundll32.exe powrprof.dll,SetSuspendState 0,1,0
and for doing reverse action, Hibernating the computer, use commands below:
powercfg -hibernate on
rundll32.exe powrprof.dll,SetSuspendState Hibernate
Only this quick sleeper works for me (it works in Windows 7, Windows 8, and Windows 10).
As a PowerShell one-liner:
(Add-Type -MemberDefinition "[DllImport(""powrprof.dll"")]`npublic static extern bool SetSuspendState(bool Hibernate, bool ForceCritical, bool DisableWakeEvent);" -Name "Win32SetSuspendState" -Namespace Win32Functions -PassThru)::SetSuspendState(0, 1, 0)
The command can be run, for instance, using Win + R.
For just turning off the display while keeping the PC on, see Turn off display in Windows on command.
16 years have passed since the original question.
If you encounter this issue now, it may be that your computer is using "Modern Standby".
Some ways this can manifest are:
Add-Type -AssemblyName System.Windows.Forms;
[System.Windows.Forms.Application]::SetSuspendState([System.Windows.Forms.PowerState]::Suspend, $false, $false)
False, and nothing happenspowercfg /A shows:
The system firmware does not support this standby stated) for the S1 and S2 statesIn this case, using Sysinternals PsShutdown with the -x option should work to put the computer into sleep mode:
.\psshutdown.exe -x
The docs for the -x option state:
-x Turn monitor off (system will initiate Modern Standby if supported)
For the other shutting down / hibernating, the shutdown command should still work.
See also: