Fixing Windows Clock Sync [AutoIt Script]

Discussion in 'Scripting' started by imlost2, May 22, 2026.

  1. imlost2

    imlost2 MDL Member

    Aug 5, 2013
    183
    184
    10
    #1 imlost2, May 22, 2026
    Last edited: May 27, 2026
    I have long been irritated by how Windows keeps PC clocks in sync. Other than surfing the Internet and retrieving email from a desktop client, NTP time checks are the most frequent connections your PC makes the Internet. The problem is that the vast majority of NPT servers are operated by volunteers, sometimes with a poor grasp on security or they are logging your ttime sync queries to their database. I attempted to solve this issue in two ways:

    First, I configured my router as the only device that connects to time servers because it supports the newer NTS (Network Time Security) standard, which fixes a decades-old security gap by using Transport Layer Security (TLS) and authenticated cryptography to prevent attackers from tampering with or spoofing time packets. I also I trust the Sweedish provider that runs the server I connect to: (Netnod): nts.ntp.se or nts.gbg.netnod.se. Windows’ built-in w32tm client does not natively support NTS yet but I wrote an AutoIt script that can retrieve the time from my router (which does) using the old Windows protocol. The code is AutoIt but you can take the AutoIt code it to your favorite chatbox and ask to convert it to a less pretty batch or powershell script:

    RouterTime.gif

    Code:
    ; =========================================================
    ; Pretty Local Router Time Sync Display
    ; Router NTP Server: 192.168.0.254
    ; =========================================================
    
    #include <Date.au3>
    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>
    
    Local $NTPServer = "192.168.0.254" ;<<<<<<<<<<Router IP goes here
    Local $IconPath = "C:\PATH-TO-BANNER-MESSAGE-ICON\Clock.ico" ;<<<<<<<<<<<< iCON PATH
    
    ; -----------------------------
    ; Capture current local time
    ; -----------------------------
    Local $BeforeTime = _NowCalc()
    
    ; -----------------------------
    ; Ping router first
    ; -----------------------------
    If Not Ping($NTPServer, 1000) Then
        MsgBox(16, "Time Sync Error", _
            "Router NTP server unreachable:" & @CRLF & _
            $NTPServer)
        Exit
    EndIf
    
    ; -----------------------------
    ; Configure Windows time source
    ; -----------------------------
    RunWait(@ComSpec & ' /c w32tm /config /manualpeerlist:"' & _
        $NTPServer & '" /syncfromflags:manual /update', "", @SW_HIDE)
    
    ; -----------------------------
    ; Force time sync
    ; -----------------------------
    Local $PID = Run(@ComSpec & ' /c w32tm /resync /force', "", _
        @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD)
    
    Local $Output = ""
    
    While 1
        $Output &= StdoutRead($PID)
        If @error Then ExitLoop
        Sleep(50)
    WEnd
    
    ; -----------------------------
    ; Wait for clock update
    ; -----------------------------
    Sleep(1000)
    
    ; -----------------------------
    ; Capture updated time
    ; -----------------------------
    Local $AfterTime = _NowCalc()
    
    ; -----------------------------
    ; Determine sync status
    ; -----------------------------
    Local $Status = "UNKNOWN"
    
    If StringInStr(StringLower($Output), "success") Then
        $Status = "SUCCESS"
    Else
        $Status = "CHECK OUTPUT"
    EndIf
    
    ; =========================================================
    ; GUI DISPLAY
    ; =========================================================
    
    Local $GUI = GUICreate( _
        "Windows Time Sync", _
        520, 280, -1, -1, _
        BitOR($WS_CAPTION, $WS_POPUPWINDOW))
    
    GUISetBkColor(0xF0F4F8)
    
    ; -----------------------------
    ; Window icon
    ; -----------------------------
    GUISetIcon($IconPath)
    
    ; -----------------------------
    ; Clock icon image
    ; -----------------------------
    GUICtrlCreateIcon($IconPath, -1, 20, 20, 64, 64)
    
    ; -----------------------------
    ; Title
    ; -----------------------------
    Local $lblTitle = GUICtrlCreateLabel( _
        "Router Time Synchronization", _
        100, 22, 360, 30)
    
    GUICtrlSetFont($lblTitle, 16, 800, 0, "Segoe UI")
    GUICtrlSetColor($lblTitle, 0x1F3A5F)
    
    ; -----------------------------
    ; NTP server label
    ; -----------------------------
    Local $lblServer = GUICtrlCreateLabel( _
        "NTP Server: " & $NTPServer, _
        100, 58, 320, 20)
    
    GUICtrlSetFont($lblServer, 10, 400, 0, "Segoe UI")
    GUICtrlSetColor($lblServer, 0x444444)
    
    ; =========================================================
    ; BEFORE TIME
    ; =========================================================
    
    Local $lblBeforeTitle = GUICtrlCreateLabel( _
        "BEFORE SYNC", _
        40, 110, 180, 24)
    
    GUICtrlSetFont($lblBeforeTitle, 12, 800, 0, "Segoe UI")
    GUICtrlSetColor($lblBeforeTitle, 0xAA0000)
    
    Local $lblBefore = GUICtrlCreateLabel( _
        $BeforeTime, _
        40, 140, 220, 30)
    
    GUICtrlSetFont($lblBefore, 15, 800, 0, "Consolas")
    GUICtrlSetColor($lblBefore, 0xCC0000)
    
    ; =========================================================
    ; AFTER TIME
    ; =========================================================
    
    Local $lblAfterTitle = GUICtrlCreateLabel( _
        "AFTER SYNC", _
        280, 110, 180, 24)
    
    GUICtrlSetFont($lblAfterTitle, 12, 800, 0, "Segoe UI")
    GUICtrlSetColor($lblAfterTitle, 0x007700)
    
    Local $lblAfter = GUICtrlCreateLabel( _
        $AfterTime, _
        280, 140, 220, 30)
    
    GUICtrlSetFont($lblAfter, 15, 800, 0, "Consolas")
    GUICtrlSetColor($lblAfter, 0x009900)
    
    ; =========================================================
    ; STATUS
    ; =========================================================
    
    Local $lblStatus = GUICtrlCreateLabel( _
        "Status: " & $Status, _
        40, 210, 420, 28)
    
    GUICtrlSetFont($lblStatus, 12, 700, 0, "Segoe UI")
    
    If $Status = "SUCCESS" Then
        GUICtrlSetColor($lblStatus, 0x008800)
    Else
        GUICtrlSetColor($lblStatus, 0xCC6600)
    EndIf
    
    ; -----------------------------
    ; Show GUI
    ; -----------------------------
    GUISetState(@SW_SHOW)
    
    ; -----------------------------
    ; Keep visible for 3 seconds
    ; -----------------------------
    Sleep(9000)
    
    GUIDelete($GUI)
    
    ; =========================================================
    ; Optional logfile
    ; =========================================================
    
    ;~ Local $LogFile = @ScriptDir & "\timesync.log"
    
    ;~ FileWrite($LogFile, _
    ;~     "========================" & @CRLF & _
    ;~     _NowCalc() & @CRLF & _
    ;~     "Before: " & $BeforeTime & @CRLF & _
    ;~     "After : " & $AfterTime & @CRLF & _
    ;~     "Status: " & $Status & @CRLF & _
    ;~     "Raw Output:" & @CRLF & _
    ;~     $Output & @CRLF & @CRLF)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...