Skip to content

Instantly share code, notes, and snippets.

@flipeador
Created April 25, 2025 21:58
Show Gist options
  • Save flipeador/ff80f0bcd4971dffdc26c44815509236 to your computer and use it in GitHub Desktop.
Save flipeador/ff80f0bcd4971dffdc26c44815509236 to your computer and use it in GitHub Desktop.
Automated adjustment of Windows Update active hours with AutoHotkey.
#Requires AutoHotkey v2
key := "HKLM\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings"
RegWrite(Mod(A_Hour + 23, 24), 'REG_DWORD', key, 'ActiveHoursStart')
RegWrite(Mod(A_Hour + 11, 24), 'REG_DWORD', key, 'ActiveHoursEnd')
RegWrite(2, 'REG_DWORD', key, 'SmartActiveHoursState')
@flipeador
Copy link
Author

Description

Programmatically modify the Windows registry to redefine the system's active hours for Windows Update.

  • Set ActiveHoursStart to one hour before the current time and ActiveHoursEnd to 11 hours after that, effectively creating a 12-hour activity window.
  • Set SmartActiveHoursState to 2, thereby disabling Windows' automatic adjustment of active hours based on usage patterns.

This preventive configuration, executed in 8-hour intervals, mitigates the risk of involuntary system reboots following update installations that require a restart, thereby preserving workflow continuity and user control.

Instructions

Install AutoHotkey v2 in %ProgramFiles%\AutoHotkey.

Place move-active-hours.ahk in %ProgramFiles%\AutoHotkey\Scripts.

Open the Windows Terminal as Administrator and run the following PowerShell script:

$TaskXml = @"
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <Triggers>
    <BootTrigger>
      <Repetition>
        <Interval>PT8H</Interval>
        <StopAtDurationEnd>false</StopAtDurationEnd>
      </Repetition>
      <Enabled>true</Enabled>
    </BootTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>true</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>false</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>"$env:PROGRAMFILES\AutoHotkey\UX\AutoHotkeyUX.exe"</Command>
      <Arguments>"$env:PROGRAMFILES\AutoHotkey\Scripts\move-active-hours.ahk"</Arguments>
    </Exec>
  </Actions>
</Task>
"@

Register-ScheduledTask -TaskPath "\AutoHotkey\" -TaskName "MoveActiveHours" -Xml $TaskXml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment