182

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?

Daniel Beck
  • 109,300
  • 14
  • 287
  • 334
Lazer
  • 17,227
  • 43
  • 116
  • 141
  • Type shutdown on the command prompt and take a look at the output works on XP and above from what I can tell. – user10547 Sep 16 '09 at 14:20
  • Related: http://superuser.com/questions/39584/what-is-the-command-to-use-to-put-your-computer-to-sleep-not-hibernate – Nayuki May 25 '16 at 06:12

13 Answers13

135

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
Dietrich Epp
  • 722
  • 6
  • 9
brandstaetter
  • 4,573
  • 2
  • 22
  • 20
  • 2
    If you have permissions you can use shutdown.exe to initiate a remote shutdown of another computer on your network. Can be good for pranks. (No responsibility taken) – Matthew Lock Apr 28 '10 at 05:33
  • 5
    Downvoted because this doesn't actually answer the question (how to trigger a sleep). Shutdown.exe does NOT trigger a sleep, only shutdown, restart or hibernate. To sleep, you need to run: rundll32.exe powrprof.dll,SetSuspendState 0,1,0 – Nicholas Head Oct 31 '10 at 18:20
  • 1
    If you read the linked page, you will find the necessary commands all listed nicely with explanations. Unwarranted downvotes FTW! :P – brandstaetter Dec 21 '10 at 12:42
  • 8
    The downvotes are not in the least unwarranted. Your answer does not address the question on its own. Readers should be able to gather enough useful information from an answer to solve the problem *without* having to follow any links to other sites which may become broken or dead. – Iszi May 19 '11 at 04:36
  • @Iszi okay, I added the info, happy now? :D – brandstaetter May 31 '11 at 08:07
  • 26
    Problem with `rundll32.exe powrprof.dll,SetSuspendState 0,1,0` is that IT DOES NOT WORK as it should. `SetSuspendState` will put the computer into a sort of Hybrid Sleep/Hibernation mode. When you use the Sleep button from the start menu, you can resume your computer from sleep by pressing a any key on your keyboard. Using this command line, makes is resume much slower due to the deeper sleep state and it can resume only if you press the power button on your computer. It cannot resume for a key press. So... how do you make it sleep as in normal standby - resumable with the press of a key? – Corporate Geek Feb 14 '10 at 12:29
  • 5
    Downvoted for not answering the question asked: even in the linked page, the method described for putting the machine to sleep won't work when hibernation is enabled. – akurtser Aug 24 '11 at 13:28
  • ... Haters gonna hate, it seems. Please provide a better solution, akurtser. – brandstaetter Aug 30 '11 at 08:50
  • 4
    I have tested the Sleep command as well, and can also confirm that it does not work as intended. It has nothing to do with hating/trolling, but if that command does not work, then the question is unanswered, since that was the main part of the question. – reSPAWNed Feb 03 '12 at 11:28
  • 1
    rundll32.exe powrprof.dll,SetSuspendState 0,1,0 makes my Win8 to hibernate (suspend-to-disk). It does not make it Suspend-to-RAM (named Sleep or Standby, depending on windows version) neither does it make go to hybrid sleep (suspend-to-RAM while at the same time save state to disk), as the Sleep option in the Shutdown menu. So the anwser (which currently it is not) should be updated. – David Balažic Aug 17 '13 at 20:20
  • 9
    `rundll32` should not be used for calling `SetSuspendState` because its signature doesn't match the one expected by `rundll32`. See: https://support.microsoft.com/en-us/kb/164787 and https://msdn.microsoft.com/en-us/library/windows/desktop/aa373201(v=vs.85).aspx The first parameter gets garbage instead of FALSE value. It's the reason, why it hibernates instead of going to sleep. – SergeyT Jul 26 '16 at 01:07
  • `PsShutdown` is a golden nugget. Haven't tested it though – Dheeraj Bhaskar Aug 10 '17 at 18:36
  • 1
    STOP PROMOTING CARGO CULT KNOWLEDGE! Stop abusing Rundll32 if you have no idea how it works, stop creating absurd workarounds, stop setting up potential system crashes. Use a proper tool, like DllCall (of AutoHotkey) which actually works the way you BELIEVE Rundll32 works. – SilverbackNet Jan 04 '18 at 03:50
  • 2
    @SilverbackNet please provide your own answer instead of hating in the comments – brandstaetter Jan 04 '18 at 07:41
  • The reasonably best correct answers were already posted by Scott Chamberlain and Eric L, even if you stole Scott's to shore up your COMPLETELY WRONG ANSWER. At least remove the part that's so wrong it burns if you're going to plagiarize something correct. – SilverbackNet Jan 06 '18 at 08:38
  • 1
    save to sleep.ahk:`DllCall("PowrProf\SetSuspendState", "Int", 0, "Int", 0, "Int", 0)` – ipcjs Dec 04 '18 at 05:39
  • After a lot of digging it seems like the only thing that works is psshutdown -x -t 0 to get the computer into Modern standby as the guide indicates. – Ahmed Apr 21 '23 at 05:24
  • This answer has worked for me perfectly fine. I don't understand what the first comment is saying, __resuming is not slow at all__. Even if I will have to press the power button only (and not other buttons), this is completely fine. Who cares? (You might have a good reason for it, I don't.) – aderchox Jun 14 '23 at 08:36
103

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

David Gardiner
  • 295
  • 3
  • 11
Scott Chamberlain
  • 30,694
  • 7
  • 96
  • 109
  • Requires elevation to Administrator – dsmtoday Feb 20 '17 at 16:00
  • 1
    Does not work under Windows 10. Operation not supported. Ran as admin. – T3rm1 Mar 23 '17 at 10:23
  • 3
    Works for me in Windows 10 x64, Build 1709. – Hans Løken Nov 29 '17 at 17:58
  • 1
    Works for me in Win10x64 Version 1709, run cmd with administrator user account but without `run as administrator` right click option – Viktor Carlson Dec 29 '17 at 08:16
  • 3
    Works for me with Windows 10 (not even as admin). – DAB May 23 '18 at 08:05
  • @T3rm1 [It works in Windows 10](https://docs.microsoft.com/en-us/sysinternals/downloads/psshutdown), but it [requires PsTools to be installed](https://download.sysinternals.com/files/PSTools.zip). – Boaz Sep 11 '18 at 18:55
  • 3
    Works in Windows 10 x64 Build 15063. One needs to [download PsTools](http://technet.microsoft.com/en-us/sysinternals/bb897541) and copy `psshutdown.exe` from the archiv to e.g. `C:\Windows\System32` in order to be able to use the command without pathprefix. One then needs to execute `psshutdown -d -t 0` with admin privileges in order to suspend. **This should be the accepted answer**, cause it answers the question, even if not with Windows-preinstalled tools. – ArchLinuxTux Sep 12 '18 at 21:24
  • Works on Windows 11 Home Build 22000 – kym Sep 05 '22 at 16:05
  • In Windows 11 Pro 22H2 this results in hibernation on a laptop which uses Modern Standby /s0 sleep. – voldemarz Nov 07 '22 at 18:16
29

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
Gaff
  • 18,569
  • 15
  • 57
  • 68
  • 6
    I think it's the first answer that actually solves it. I put these two lines (-hibernate off first) and made a batch file that puts the pc to sleep. One problem with that is that it essentially turns off hibernation, which is not what I wanted, so to fix it, I added a scheduled task triggered on "on workstation unlock" to turn the hibernation back on with a batch whose only line is "powercfg -hibernate off". Then I configured a short key for the first batch file. Done. Works like a charm, thank you! – akurtser Aug 24 '11 at 11:44
  • That is so weird. Hibernation needs to be turned off before you can sleep properly? Is there a name for the hybrid sleep/suspend state `powrprof.dll` is when hibernate is on? Just want to know. Does it relate to any of the ACPI states? – Ehtesh Choudhury Sep 12 '12 at 16:19
  • 5
    `rundll32` should not be used for calling `SetSuspendState` because its signature doesn't match the one expected by `rundll32`. See: https://support.microsoft.com/en-us/kb/164787 and https://msdn.microsoft.com/en-us/library/windows/desktop/aa373201(v=vs.85).aspx The first parameter gets garbage instead of FALSE. It's the reason, why it hibernates instead of going to sleep. – SergeyT Jul 26 '16 at 01:10
  • Does not work under Windows 10 – T3rm1 Mar 23 '17 at 10:22
  • @EhteshChoudhury, the problem in this case is that the Rundll32 parameters are incorrect and completely unsuited, so that they actually call SetSuspendState with the parameters: Hibernate: TRUE, ForceCritical: TRUE, DisableWakeEvents: TRUE. The last bit explains why you can't wake the PC with anything but the power button. It's only a coincidence that trying to hibernate when it's disabled will put your PC to sleep in Win7, and probably why it doesn't work for some people. – SilverbackNet Jan 04 '18 at 04:03
  • @SergeyT so whats your suggestion? Is there any other useful commands could be used rather than rundll32? – Mojtaba Rezaeian Oct 03 '21 at 03:13
  • @MojtabaRezaeian I'd create a small program as it is suggested here https://superuser.com/a/1172869/472715 or maybe try PowerShell to call `SetSuspendState` properly, see https://superuser.com/a/1310274/472715 – SergeyT Oct 05 '21 at 19:53
13

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.

Eric L
  • 1,433
  • 3
  • 15
  • 16
  • 2
    This works without having to elevate to Administrator. Works with or without hibernate enabled. Works as expected with hybrid sleep. Nice solution for Win10. – dsmtoday Feb 20 '17 at 16:38
  • Excellent! If anyone wants the delay, it supports in in milliseconds instead of seconds: `nircmd cmdwait 15000 standby` will wait 15 seconds before sleeping. And if you just run `nircmd.exe` as admin without any arguments, it has a convenient button to copy itself to system32. – Allon Guralnek Mar 18 '22 at 11:19
  • In Windows 11 Pro 22H2 this results in hibernation on a laptop which uses Modern Standby /s0 sleep. – voldemarz Nov 07 '22 at 18:16
  • In Windows 11 `nircmd.exe monitor off` puts the laptop to sleep even if its is set to allow hibernation. Tested on HP laptop with Windows 11 Pro only – JMax Feb 25 '23 at 23:44
10

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();"
AveYo
  • 395
  • 5
  • 10
  • I have windows 10 pro it puts system in sleep mode and gets back in about 3 seconds, what could be the reason?? – sairfan Mar 07 '23 at 04:05
6

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.

Gaff
  • 18,569
  • 15
  • 57
  • 68
BlueDevil
  • 426
  • 1
  • 4
  • 10
  • 1
    `rundll32` should not be used for calling `SetSuspendState` because its signature doesn't match the one expected by `rundll32`. See: https://support.microsoft.com/en-us/kb/164787 and https://msdn.microsoft.com/en-us/library/windows/desktop/aa373201(v=vs.85).aspx The first parameter gets garbage instead of FALSE, so it always hibernates instead of going to sleep. – SergeyT Jul 26 '16 at 01:38
  • This solution works for me since I don't have hibernate enabled. Thanks. – ScrappyDev Apr 23 '22 at 05:50
4

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 sorta 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.

dash-tom-bang
  • 266
  • 1
  • 8
4

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.

Damn Vegetables
  • 3,622
  • 16
  • 46
  • 78
4

See the free utility of Wizmo, which can do very many things.
The command you're looking for is probably:

wizmo standby

harrymc
  • 455,459
  • 31
  • 526
  • 924
  • 1
    Wizmo has the same problem as `rundll32.exe powrprof.dll,SetSuspendState 0,1,0` -- it puts the computer in hibernation, not in sleep – Andomar Aug 19 '12 at 18:53
  • 2
    No, since Vista it actually puts the computer in [Hybrid Sleep](http://www.mydigitallife.info/what-is-hybrid-sleep-and-differences-with-basic-sleep-mode-in-vista/). If you don't want this, then [Turn Off Hybrid Sleep](http://maximumpcguides.com/windows-7/turn-off-hybrid-sleep-in-windows-7/). – harrymc Aug 19 '12 at 20:26
  • 1
    wizmo blackout worked in windows 8.1, 10. GREAT!!!!!!!!!!!!! – YumYumYum Jul 14 '16 at 15:46
  • Wizmo work great. Unfortunately my enterprise virus scanner deletes it as it contains some sequences that make mcafee think it [contains a trojan](https://www.virustotal.com/en/file/7b0b47f936d24686de461bea05a9480179035a5b2b23a74167a7728e95922d5d/analysis/). – bastian Oct 26 '17 at 09:47
  • @bastian: Mcafee was always bad news for an application writer (from personal experience). – harrymc Oct 26 '17 at 10:04
3

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
Synetech
  • 68,243
  • 36
  • 223
  • 356
Mohsen Abasi
  • 173
  • 2
  • 3
    `rundll32` is not intended for calling `SetSuspendState` and should not be used for this. The first parameter gets garbage instead of correct value. So it always hibernates instead of going to sleep (if you have not disabled hibernation at all). – SergeyT Jul 26 '16 at 22:12
  • @SergeyT It seems to be working for me (after disabling hibernation); are you saying this will corrupt windows? – moondra Jul 04 '17 at 16:17
  • 2
    It corrupts the stack (since rundll32 expects a different call signature, so it pops things off the stack that haven't actually been put there). This leads to unexpected behaviour. – imoatama Mar 23 '18 at 00:51
  • @imoatama nice finding... – masterxilo Aug 16 '18 at 13:27
  • @moondra no, in this case it won't corrupt Windows. It just won't do what you've expected. – SergeyT Oct 05 '21 at 19:48
1

only QUICK SLEEPER works for me (works in Windows 7,8,10).

p.s. source code available here.

T.Todua
  • 3,815
  • 9
  • 42
  • 57
  • 2
    Is there any chance you can provide a link to the actual site that you got the file from? Linking directly to the download file, especially when using g a URL shortener, can make the file seem particularly suspicious as it is a tactic often employed by malware writers. – Mokubai Mar 20 '16 at 07:01
  • ok, i have updated link to official site. – T.Todua Apr 20 '16 at 13:51
1

Here's an article on using CLI to shutdown.

Microsoft provides PsTools which gives you CLI shutdown along with other useful tools. You can get that here.

iopq
  • 99
  • 3
wonton
  • 385
  • 1
  • 3
  • 15
0

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)

Command can be run for instance using Win + R.

For just turning off display while keeping PC on, see https://superuser.com/a/1735341/155147.

Jari Turkia
  • 647
  • 8
  • 13