22 Votes

Put Windows via Script into Hibernation or Sleep Mode

Tutorial by Stefan Trost | 2024-04-24 at 17:42

In this tutorial, we want to look at how we can put our Windows computer either into its hibernation state or in its sleep mode by using a script respectively with the help of a command line command. We first look at the general command for activating both of these two states and then we extend this to include an optional time delay.

In the tutorial about shutting down a Windows computer using a batch script, we have already looked at how we can control the Windows system program "shutdown.exe" to shut down or restart our Windows computer. To shut down, we can call shutdown.exe with the parameter s (which stands for "shutdown"), to restart the system we can use the parameter r instead, which stands for the word "restart".

Hibernation

The equivalent parameter to hibernate our computer is the "h" standing for "hibernation".

The hibernation call is made in the same way as the other examples we already know from the shutdown tutorial:

shutdown.exe -h

With this command we will put our computer into immediate hibernation mode.

Sleep Mode

Although shutdown.exe can be controlled with a variety of parameters and can be used for shutdowns and restarts as well as for hibernations, the program does not have a parameter for activating the sleep mode. For this reason we have to proceed differently to activate it:

runDLL32.exe powrprof.dll,SetSuspendState Sleep

The progam runDLL32.exe is a Windows utility that can be used to execute Windows functions defined in a DLL (program library). Here, we are using runDLL32 to call the function SetSuspendState() from the DLL powrprof.dll, which can put our computer into both hibernation and sleep mode. Since we are only interested in the sleep mode at this point, we pass “Sleep” as an additional parameter to the function.

Time Delay

Following the logic, it would be reasonable to assume that we can add a time delay to "shutdown.exe -h" by just appending a t-value (time) as an additional parameter, as we already know it from our shutdown and restart examples.

However, "shutdown.exe" does not provide this for the "hibernation". In all of my tests for this tutorial using different versions of Windows, commands like "shutdown.exe -h -t 60" were not accepted and were only acknowledged with an error message. Although, on the same computers, the same commands worked for the delayed shutdown ("shutdown.exe -s -t 60") as well as for the delayed restart ("shutdown.exe -r -t 60") after a one minute (60 seconds) delay.

If we nevertheless want to send our computer to sleep with a time delay, we have to use a workaround. For example, we can set a timeout into our script before calling the command. Our next example shows how this works for a timeout of one minute (60 seconds):

timeout /t 60 /nobreak
shutdown.exe -h

Calling these lines ensures that first, the countdown is carried out for 60 seconds and only then shutdown.exe is called and thus the hibernation state is activated. Instead of the 60 seconds used here, we can of course also use any other number of seconds for our break.

The situation is similar with activating the sleep respectively energy saving mode. The SetSuspendState() function from the powrprof DLL also has no parameter for specifying a time. So, in this case too we have to work with a timeout that is independent of SetSuspendState():

timeout /t 180 /nobreak
runDLL32.exe powrprof.dll,SetSuspendState Sleep

These two lines will put our computer into sleep mode after a pause of 3 minutes (180 seconds).

Automatic Program Closing

In the already mentioned tutorial about the script-controlled shutdown or restart of Windows, we have also looked at the f parameter, with which we can force all running applications to be closed without prompting before shutdown, regardless of whether the programs still have open files with unsaved changes.

This parameter can also be used in conjunction with the hibernation state in order to close running programs before starting the hibernation. All we have to do for this, is to pass the option "f" as an additional parameter:

shutdown.exe -h -f

With this command we send our computer to hibernation and close all open programs beforehand. Changes to open files that have not yet been saved will get lost.

Requirements

Finally, it should be mentioned that the commands shown in this tutorial only work if the hibernation respectively the sleep mode is activated on the computer in question.

You can easily find out whether this is the case by checking whether the menu for shutting down the computer, in addition to "Shutdown" and "Restart", also comes with the options "Hibernate" respectively "Sleep".

If this is not the case, the hibernation can usually be activated in the Control Panel if your system generally supports that state. In the case of virtual machines, for example, this is usually not possible.

How that activation works, I have summarized for you step by step in the tutorial on making Windows hibernation mode available. The sleep mode can be made available in the same way.

Alternative Calling Method

On current Windows versions, the program runDLL32.exe as well as the program shutdown.exe are located in the folder "system32" of the Windows installation. In the case of a default Windows installation, this is the folder "C:\Windows\System32". So, we can also call both programs via this full path respectively via its system variable %SYSTEMROOT% in the form "%SYSTEMROOT%\System32".

At the same time, we can also define the parameters of shutdown.exe using a slash instead of the hyphen shown here. That is, "shutdown /h" instead of "shutdown -h".

For examples of this alternative method of calling, see the Windows Shutdown Tutorial. In that tutorial, you will also find other more deeper explanations as well as additional parameters for shutdown.exe.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

VirtualBox: Change Date and Time

Tutorial | 10 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.