Skip to main content

How to Put the Computer to Sleep or Hibernate in Batch Script

Automating power management is a useful feature for scripts that run at the end of the day or complete long-running tasks overnight. Instead of leaving the computer running, a script can automatically put it into a low-power state like Sleep or a zero-power state like Hibernate to save energy.

This guide will teach you the standard, built-in commands to initiate both Sleep and Hibernate from a batch script. You will learn the modern shutdown command for hibernation and the classic rundll32 command for putting the computer to sleep, as well as the important prerequisites for these commands to work correctly.

Sleep vs. Hibernate: What's the Difference?

  • Sleep: Puts your computer into a low-power state. The memory (RAM) is kept powered on to save your open session. Resuming is very fast (a few seconds). However, if power is lost (e.g., the battery dies), your session is lost.
  • Hibernate: Saves the entire content of your memory to a file on the hard disk (hiberfil.sys) and then turns the computer completely off, consuming zero power. Resuming is slower than sleep but your exact session is restored, even if power is completely disconnected.

The Core Command for Hibernation: shutdown /h

The modern and recommended way to initiate hibernation is with the versatile shutdown.exe command.

Syntax: shutdown /h

  • /h: This switch instructs the command to Hibernate the local computer.

This command is simple, reliable, and the official method for hibernation.

The Core Command for Sleep: rundll32.exe

Windows has no direct, single command like sleep.exe. Instead, the standard method is to use the rundll32.exe utility, which can execute functions from system DLL files. We use it to call the power management function SetSuspendState.

Syntax: rundll32.exe powrprof.dll,SetSuspendState Sleep

  • rundll32.exe: The utility to run a DLL function.
  • powrprof.dll: The Power Profile helper DLL that contains the function.
  • ,SetSuspendState: The name of the function to call.
  • Sleep: The state we want to enter.

Basic Example: A Simple Sleep/Hibernate Script

This script gives the user a choice of which power state to enter.

danger

Save your work before running this script.

@ECHO OFF
TITLE Power Management Script
ECHO --- Choose a Power Option ---
ECHO.
ECHO 1. Put the computer to Sleep
ECHO 2. Hibernate the computer
ECHO.

CHOICE /C 12 /M "Enter your choice: "

IF %ERRORLEVEL% EQU 2 GOTO :DoHibernate
IF %ERRORLEVEL% EQU 1 GOTO :DoSleep
GOTO :EOF

:DoSleep
ECHO Putting the computer to sleep in 5 seconds...
TIMEOUT /T 5
rundll32.exe powrprof.dll,SetSuspendState Sleep
GOTO :EOF

:DoHibernate
ECHO Hibernating the computer in 5 seconds...
TIMEOUT /T 5
shutdown /h
GOTO :EOF

How the commands work:

  • shutdown /h: This is a high-level command that safely signals the operating system to begin the hibernation process. It saves memory to the hibernation file and powers down the machine.
  • rundll32.exe powrprof.dll,SetSuspendState Sleep: This is a more direct, low-level call. It tells the power management API to transition the system to the suspend state (S3 sleep state), which keeps RAM powered on while shutting down most other components.

Common Pitfalls and How to Solve Them

Problem: The Hibernate Command Fails

The most common issue with shutdown /h is that it fails with an error or does nothing. This is almost always because hibernation is disabled on the system.

Example error message:

The request is not supported.(50)

Solution: You must enable hibernation first. This requires administrator privileges.

REM This command enables the hibernation feature.
powercfg /hibernate on
note

You can put this command at the beginning of your script to ensure the feature is available.

Problem: The rundll32 Command Hibernates Instead of Sleeping

You run the rundll32.exe ... Sleep command, but the computer hibernates instead. This happens if the Hybrid Sleep feature is enabled in your power plan. Hybrid Sleep is a state that combines sleep and hibernation, it saves your session to both RAM and the hard disk, so you can resume quickly but are also protected from power loss.

Solution: This is expected behavior for that power setting. If you need a "pure" sleep state, you must disable Hybrid Sleep in the advanced power options in the Windows Control Panel. For most users, Hybrid Sleep is a good feature, so it's often best to leave it as is.

Problem: The Action Happens Too Quickly

Running shutdown /h or the rundll32 command is instant. This can be jarring for a user.

Solution: Always provide a countdown using the TIMEOUT command before changing the power state. This gives the user a chance to read the message and abort if they ran the script by accident.

ECHO Hibernating in 10 seconds. Press Ctrl+C to cancel.
TIMEOUT /T 10
shutdown /h

Practical Example: A "Finish Work and Hibernate" Script

This script simulates a long-running task (like a backup or a render). Once the task is complete, it automatically hibernates the computer.

@ECHO OFF
SETLOCAL
TITLE Automated Task Runner

ECHO --- Starting long-running backup process ---
ECHO The computer will hibernate automatically when this is done.
ECHO.

REM --- Simulate a long task (e.g., 30 minutes) ---
ECHO Backup in progress...
TIMEOUT /T 1800

ECHO.
ECHO [SUCCESS] The backup is complete.
ECHO.

REM --- Check if hibernation is enabled first ---
powercfg /a | find "Hibernation" > NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO [WARNING] Hibernation is not available on this system.
ECHO The computer will not be shut down.
GOTO :End
)

ECHO Hibernating the computer in 60 seconds.
TIMEOUT /T 60
shutdown /h

:End
ENDLOCAL

Conclusion

Automating sleep and hibernate is a great way to make your scripts more convenient and energy-efficient.

Key takeaways:

  • For Hibernation, use the modern shutdown /h command. It's simple and reliable.
  • For Sleep, use the standard rundll32.exe powrprof.dll,SetSuspendState Sleep command.
  • Before hibernating, ensure the feature is enabled with powercfg /hibernate on (requires admin rights).
  • Always use a TIMEOUT command to give the user a warning and a chance to cancel before changing the power state.