How to Set Active Hours for Windows Update in Batch Script
Windows Update "Active Hours" is a feature designed to prevent your computer from automatically restarting while you are working. By defining a specific range of time when the computer is in use, Windows ensures that updates requiring a reboot are only applied outside of these hours. For developers, office workers, and students, automating the configuration of these hours via a Batch script is a professional way to ensure personal or team productivity is never interrupted by an unexpected update.
This guide explains how to modify the Windows registry to set your custom start and end times for active hours.
Why Customize Active Hours?
- Prevent Downtime: Ensuring restarts only happen late at night or during off-peak times.
- Project Stability: Protecting long-running tasks, such as code compilation or data processing, from being cut short.
- Team Consistency: Deploying standardized "Work Hours" across all machines in a small office or lab environment.
When you modify the active hours via a Batch script (Registry), the changes are picked up by the Update Orchestrator service almost immediately. You do not need to reboot for the new hours to be respected.
Understanding the Registry Keys
Windows stores Active Hours settings in the following registry path:
HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings
The two key values are:
- ActiveHoursStart: The hour the workday begins (0–23).
- ActiveHoursEnd: The hour the workday ends (0–23).
The difference between the two values must not exceed 18 hours. Windows will silently ignore or revert values that violate this limit.
Modifying the HKLM hive requires full system permissions. You MUST run your Batch script as an Administrator, or the reg add commands will fail.
Creating the Active Hours Configuration Script
The following script sets typical work hours (e.g., 8:00 AM to 6:00 PM) for the Windows Update service.
@echo off
setlocal EnableDelayedExpansion
echo ============================================================
echo Update Scheduler: Active Hours Configurator
echo ============================================================
:: 1. Check for Administrative Privileges
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] This script must be run as Administrator.
pause
exit /b 1
)
:: 2. Define desired hours (edit these two values)
set "START_HOUR=8"
set "END_HOUR=18"
:: 3. Validate the range (max 18-hour span)
set /a "SPAN=END_HOUR - START_HOUR"
if !SPAN! lss 0 set /a "SPAN=SPAN + 24"
if !SPAN! gtr 18 (
echo [ERROR] Active Hours span is !SPAN! hours. Maximum allowed is 18.
pause
exit /b 1
)
if !SPAN! equ 0 (
echo [ERROR] Start and End hours cannot be the same.
pause
exit /b 1
)
set "REG_PATH=HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings"
:: 4. Set Start Time
echo [PROCESS] Setting Start Time to %START_HOUR%:00...
reg add "%REG_PATH%" /v "ActiveHoursStart" /t REG_DWORD /d %START_HOUR% /f >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Failed to set ActiveHoursStart.
pause
exit /b 1
)
:: 5. Set End Time
echo [PROCESS] Setting End Time to %END_HOUR%:00...
reg add "%REG_PATH%" /v "ActiveHoursEnd" /t REG_DWORD /d %END_HOUR% /f >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Failed to set ActiveHoursEnd.
pause
exit /b 1
)
:: 6. Disable Smart Active Hours so manual values are respected
reg add "%REG_PATH%" /v "SmartActiveHoursState" /t REG_DWORD /d 2 /f >nul 2>&1
:: 7. Verify the settings by reading them back
echo.
echo [VERIFY] Reading back current Active Hours values:
for %%V in (ActiveHoursStart ActiveHoursEnd SmartActiveHoursState) do (
reg query "%REG_PATH%" /v "%%V" 2>nul | findstr /I "%%V"
)
echo.
echo [SUCCESS] Your computer will NOT automatically restart
echo between %START_HOUR%:00 and %END_HOUR%:00.
echo ============================================================
pause
Explaining the Hour Values
- The value is a decimal integer representing the hour on a 24-hour clock.
8= 8:00 AM.18= 6:00 PM.22= 10:00 PM.- To change the active hours, edit the
START_HOURandEND_HOURvariables at the top of the script.
Explaining Smart Active Hours
Windows can automatically adjust your active hours based on device-usage patterns. The SmartActiveHoursState value controls this:
0or1= Smart Active Hours enabled (Windows may override your manual settings).2= Smart Active Hours disabled (your manual values are always respected).
The script sets this to 2 so that the hours you configure are never silently overridden.
Common Pitfalls and How to Avoid Them
Max Range Limitation
Windows imposes a maximum limit on how long your "Active Hours" can be. In current versions of Windows 10/11, the maximum duration is 18 hours.
Wrong Way:
:: Trying to set a 20-hour window (8 AM to 4 AM = 20 hours)
set "START_HOUR=8"
set "END_HOUR=4"
:: Windows will silently ignore this or revert to defaults.
Correct Way: Ensure your range is 18 hours or less. The script above validates this automatically. If you need 24-hour protection, consider disabling automatic restarts entirely via Group Policy instead.
Smart Active Hours Overriding Your Settings
Windows has a feature called "Smart Active Hours" that tries to guess your schedule based on device usage. If this is enabled, Windows may silently change your start and end times, making it appear that your script had no effect.
Wrong Way:
:: Setting hours without disabling Smart Active Hours
reg add "%REG_PATH%" /v "ActiveHoursStart" /t REG_DWORD /d 8 /f
reg add "%REG_PATH%" /v "ActiveHoursEnd" /t REG_DWORD /d 18 /f
:: Windows may override these values within hours based on usage patterns.
Correct Way:
:: Disable Smart Active Hours so your values stick
reg add "%REG_PATH%" /v "SmartActiveHoursState" /t REG_DWORD /d 2 /f >nul 2>&1
reg add "%REG_PATH%" /v "ActiveHoursStart" /t REG_DWORD /d 8 /f >nul 2>&1
reg add "%REG_PATH%" /v "ActiveHoursEnd" /t REG_DWORD /d 18 /f >nul 2>&1
Checking Only the Last %errorlevel%
If your script runs two reg add commands but only checks %errorlevel% once at the end, a failure in the first command is silently hidden by the success of the second.
Wrong Way:
reg add "%REG_PATH%" /v "ActiveHoursStart" /t REG_DWORD /d 8 /f
reg add "%REG_PATH%" /v "ActiveHoursEnd" /t REG_DWORD /d 18 /f
:: Only reflects the result of ActiveHoursEnd
if %errorlevel% equ 0 echo [SUCCESS]
Correct Way:
Check %errorlevel% immediately after each critical command, as shown in the main script above.
Always read back the values you just wrote using reg query to give the user visible confirmation. A successful reg add exit code only means the command ran, it does not prove the value persists if a Group Policy immediately overwrites it.
Best Practices for Scheduling
- Overnight Restarts: Always leave a window of at least 6 hours (e.g., Midnight to 6 AM) for the system to perform its maintenance.
- Combine with Restart Notifications: Even with active hours set, enable "Restart Notifications" in Settings so you are not surprised by a reboot right at the boundary of your configured hours.
- Auditing: Read back all Active Hours values to verify they were set correctly:
echo --- Current Active Hours ---for %%V in (ActiveHoursStart ActiveHoursEnd SmartActiveHoursState) do (reg query "%REG_PATH%" /v "%%V" 2>nul | findstr /I "%%V")
- Make Hours Easy to Change: Keep the start and end values in variables at the top of the script (as shown above) so anyone can customize the hours without editing registry paths deep in the code.
Note that on some Pro and Enterprise versions of Windows, these settings can be overridden by Domain Group Policies. If your script runs successfully but the hours in the Settings app don't change, your IT administrator may have locked the settings.
Conclusion
Setting Active Hours via a Batch script is a simple, non-intrusive way to manage the Windows Update experience without completely disabling security patches. By precisely defining your work schedule in the registry, you protect your active sessions from being interrupted while still allowing the operating system to maintain itself during off-hours. This professional level of control ensures that your computer works around your schedule, rather than forcing you to work around its maintenance cycles, providing a seamless and productive user experience throughout your designated workday.