How to Set Display and Sleep Timeout Values in Batch Script
Configuring power management settings, specifically when the display turns off and when the computer goes to sleep, is a fundamental task for system administrators. Whether you are deploying laptops that need aggressive battery-saving policies or setting up a desktop that must run long calculations overnight without sleeping, using the Windows powercfg command line utility within a Batch Script is the most reliable method.
In this guide, we will explore how to interact with the active power plan to adjust display and sleep timeouts for both AC (plugged in) and DC (battery) power states.
Understanding the POWERCFG Command
The powercfg command is the native Windows tool for controlling power schemes. Unlike registry hacks that may not apply until a reboot, powercfg changes take effect instantly.
We will focus on two specific settings within powercfg:
monitor-timeout-ac/monitor-timeout-dc: Controls when the screen turns off.standby-timeout-ac/standby-timeout-dc: Controls when the computer enters sleep mode.
The values provided to these commands are always in minutes. Setting a value to 0 means "Never."
Checking Current Settings
Before making changes, you can view the active settings using:
@echo off
powercfg /q > "%temp%\power_settings.txt"
notepad "%temp%\power_settings.txt"
del "%temp%\power_settings.txt"
The output of powercfg /q is verbose and contains the GUIDs (Globally Unique Identifiers) for every power setting. Fortunately, Windows provides shortcut aliases (like monitor-timeout-ac) so we don't have to memorize GUIDs for basic changes.
Method 1: Changing the Active Power Scheme Timeouts
The simplest approach is to modify the settings of whatever power plan is currently active on the machine.
@echo off
setlocal
echo Configuring Display and Sleep Timeouts...
echo =========================================
:: Set values in minutes (0 = Never)
set "disp_plugged=15"
set "disp_battery=5"
set "sleep_plugged=0"
set "sleep_battery=10"
echo 1. Setting Display Timeouts...
powercfg /change monitor-timeout-ac %disp_plugged%
powercfg /change monitor-timeout-dc %disp_battery%
echo 2. Setting Sleep Timeouts...
powercfg /change standby-timeout-ac %sleep_plugged%
powercfg /change standby-timeout-dc %sleep_battery%
echo.
echo [OK] Power settings updated.
echo Display off: %disp_plugged%m (Plugged^), %disp_battery%m (Battery^)
echo Sleep: %sleep_plugged%m (Plugged^), %sleep_battery%m (Battery^)
pause
Explanation of the Settings
In the script above:
- When plugged into the wall (
-ac), the screen turns off after 15 minutes, but the computer never goes to sleep (0). - When on battery (
-dc), the screen turns off after 5 minutes, and the laptop sleeps after 10.
Even though these commands don't require Administrator privileges to run, it is highly recommended to run power-configuration scripts as an Admin to ensure group policies or restricted user environments do not block the changes.
Method 2: The High-Performance Computing Setup (Never Sleep)
For servers, rendering machines, or kiosks, you often want to ensure the machine never powers down its display or goes to sleep, regardless of whether it accidentally drops to battery backup power.
@echo off
echo Enforcing "Always On" Power Policy...
:: Prevent Display from turning off
powercfg /change monitor-timeout-ac 0
powercfg /change monitor-timeout-dc 0
:: Prevent System Sleep
powercfg /change standby-timeout-ac 0
powercfg /change standby-timeout-dc 0
:: Prevent Hard Disk from turning off (Optional but recommended)
powercfg /change disk-timeout-ac 0
powercfg /change disk-timeout-dc 0
:: Prevent Hibernation
powercfg /change hibernate-timeout-ac 0
powercfg /change hibernate-timeout-dc 0
echo [OK] System will never sleep.
pause
This "shotgun" approach ensures maximum uptime for unattended processes. By setting the disk-timeout to 0, mechanical hard drives will not spin down, preventing latency spikes when they spin back up.
Method 3: Temporarily Preventing Sleep During a Task
Sometimes you only want to prevent sleep while a specific script is running, and then return to the original power settings afterward. The most reliable approach is to use the powercfg /requests system combined with powercfg /requestsoverride, or more simply, to save the current timeout values, set them to zero, run the task, and restore them.
However, the cleanest method uses the built-in powercfg /setdcvalueindex and /setacvalueindex commands on the active scheme. A simpler approach for temporary prevention is to briefly switch to the "High Performance" plan during execution, then switch back.
@echo off
setlocal enabledelayedexpansion
echo Getting current active power scheme...
:: Find the active scheme GUID
set "ActiveScheme="
for /f "tokens=4" %%a in ('powercfg /getactivescheme') do (
set "candidate=%%a"
REM Verify it looks like a GUID (contains hyphens)
echo !candidate! | find "-" >nul
if !errorlevel! equ 0 set "ActiveScheme=!candidate!"
)
if not defined ActiveScheme (
echo [ERROR] Could not determine the active power scheme.
pause
exit /b 1
)
echo Currently using: !ActiveScheme!
:: The well-known GUID for High Performance
set "HighPerf=8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"
:: Verify High Performance plan exists
powercfg /query !HighPerf! >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] High Performance plan not available on this system.
echo Falling back to manual timeout override...
powercfg /change standby-timeout-ac 0
powercfg /change standby-timeout-dc 0
goto run_task
)
echo Switching to High Performance...
powercfg /setactive !HighPerf!
:run_task
echo ----------------------------------------
echo Running critical, long-duration task...
echo Do not close this window.
echo ----------------------------------------
timeout /t 10 /nobreak >nul
:: [Your actual long task goes here]
echo Task complete. Reverting power scheme...
powercfg /setactive !ActiveScheme!
echo [OK] Original power settings restored.
pause
If the user's "High Performance" plan was previously customized to still allow sleep, this method may fail to keep the PC awake. For mission-critical tasks, explicitly set the timeouts to 0 using Method 2 commands before the task begins, and restore them afterward.
Common Mistakes
The Wrong Way: Confusing Seconds and Minutes
:: WRONG - The user intended 5 minutes, but the computer sleeps in 300 minutes (5 hours)
powercfg /change standby-timeout-ac 300
Output Concern:
Unlike the screen saver timeout registry keys (which use seconds), powercfg /change always expects values in minutes. A value of 300 minutes means the PC will stay awake all day before sleeping.
The Wrong Way: Forgetting the Battery State (-dc)
:: WRONG - Only sets the plugged-in policy
powercfg /change standby-timeout-ac 0
If you deploy this to a laptop, and the user unplugs it to move to a conference room, the laptop will still go to sleep based on the default battery policy (usually 15 minutes). Always configure both -ac and -dc states unless you are absolutely certain the target is a desktop without a UPS.
The Correct Way: Complete Configuration
powercfg /change standby-timeout-ac 0
powercfg /change standby-timeout-dc 0
Creating a Power Plan from Scratch
For advanced deployments, altering the active plan is frowned upon because users can reset it. Instead, create a custom power plan, configure its timeouts, and set it as active.
@echo off
setlocal enabledelayedexpansion
:: Duplicate the "Balanced" scheme
set "NEW_GUID="
for /f "tokens=4" %%G in ('powercfg /duplicatescheme 381b4222-f694-41f0-9685-ff5bb260df2e') do (
set "candidate=%%G"
echo !candidate! | find "-" >nul
if !errorlevel! equ 0 set "NEW_GUID=!candidate!"
)
if not defined NEW_GUID (
echo [ERROR] Failed to create new power scheme.
pause
exit /b 1
)
:: Rename the new scheme
powercfg /changename !NEW_GUID! "Kiosk Presenter" "Custom power plan for kiosk displays."
:: Set the new scheme as active
powercfg /setactive !NEW_GUID!
:: Configure the timeouts on the new active plan
powercfg /change monitor-timeout-ac 0
powercfg /change standby-timeout-ac 0
echo [OK] Custom "Kiosk Presenter" power plan created and activated.
echo GUID: !NEW_GUID!
pause
Conclusion
Adjusting display and sleep timeout values in Batch Script is easily accomplished using the powercfg /change arguments. By explicitly defining the minute values for both the active (-ac) and battery (-dc) states, system administrators can programmatically enforce energy-saving policies on laptops or guarantee uninterrupted execution for continuous desktop processes. Operating directly on the active power plan is the most effective approach for immediate, reboot-free power management.