Skip to main content

How to Sync the System Clock with a Time Server in Batch Script

Maintaining an accurate system clock is crucial for a healthy computer. Correct timestamps are essential for log file analysis, file synchronization, and, most importantly, for security protocols like Kerberos, which can fail if the client's clock is out of sync with the server's. While Windows automatically syncs its time periodically, a script might need to force an immediate synchronization to ensure accuracy before a critical task.

This guide will teach you how to use the w32tm.exe utility, the standard command-line tool for the Windows Time service. You will learn the three-step process to configure, update, and trigger a time sync from a batch script, and the critical prerequisites required for the command to succeed.

The Core Command: w32tm (Windows Time)

The w32tm.exe utility is the command-line interface for the Windows Time service (w32time), which is responsible for all time synchronization on the OS. While the older net time command exists, w32tm is the modern, more powerful, and recommended tool.

This process requires administrator privileges as it modifies system-level configurations.

The Three-Step Process for a Forced Sync

Forcing a sync with an external time source is a sequence of three commands:

  1. Configure the Time Source: First, you must tell the Windows Time service which server(s) to contact. You can use public NTP (Network Time Protocol) servers for this. w32tm /config /manualpeerlist:"pool.ntp.org" /syncfromflags:manual /reliable:yes
  2. Update the Configuration: After changing the configuration, you must tell the service to apply the changes. w32tm /config /update
  3. Trigger the Sync: Finally, you tell the service to perform the synchronization now. w32tm /resync

Basic Example: A Simple Time Sync Script

This script executes the three core commands in sequence to sync the clock. It must be run as an Administrator.

@ECHO OFF
ECHO --- Forcing System Clock Synchronization ---
ECHO.

ECHO Step 1: Configuring the NTP server to 'pool.ntp.org'...
w32tm /config /manualpeerlist:"pool.ntp.org" /syncfromflags:manual /reliable:yes

ECHO Step 2: Applying the new configuration...
w32tm /config /update

ECHO Step 3: Triggering the time synchronization...
w32tm /resync

ECHO.
ECHO --- Time sync commands have been sent ---

How the command works:

  • w32tm /config /manualpeerlist:"...": This command sets the list of servers (peers) that your computer will contact. pool.ntp.org is a popular, public pool of time servers.
  • /syncfromflags:manual: This tells the service to use the manual peer list you just provided.
  • w32tm /config /update: This signals the w32time service that its configuration has changed in the registry and that it should reload it. This step is mandatory; without it, the service will keep using the old settings.
  • w32tm /resync: This command instructs the service to immediately attempt to contact the configured peer and synchronize the clock. This command will set %ERRORLEVEL% to 0 on success and a non-zero value on failure.

Common Pitfalls and How to Solve Them

Problem: The Script is Not Run as Administrator

Configuring the Windows Time service is a privileged operation.

Example of error message:

The following error occurred: Access is denied. (0x80070005)

Solution: The script must be run from an elevated command prompt. Right-click your .bat file or cmd.exe and select "Run as administrator."

Problem: The Windows Time Service is Not Running

The w32tm command is just a controller; it sends commands to the w32time service. If this service is stopped or disabled, the commands will fail.

Example of error message:

The service has not been started. (0x80070426)

Solution: A robust script should check if the service is running and start it if it's not.

SC query w32time | FIND "STATE" | FIND "RUNNING" > NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO Windows Time service is not running. Starting it...
NET START w32time
)

Problem: A Firewall is Blocking the Connection (NTP Port)

The Network Time Protocol (NTP) communicates over UDP port 123. If a corporate or local firewall is blocking outgoing traffic on this port, the w32tm /resync command will fail, often with a "The computer did not resync because no time data was available" message.

Solution: Ensure that outbound UDP port 123 is allowed in your firewall configuration. This is a network configuration issue, not a script error.

Practical Example: A Robust Pre-Task Sync Script

This script includes all the best practices: it checks for administrator rights, ensures the time service is running, and then performs the sync, reporting on the final success or failure.

@ECHO OFF
SETLOCAL
TITLE System Time Synchronization

ECHO --- Robust Time Sync Script ---
ECHO.

REM --- Step 1: Check for Administrator Privileges ---
NET session >nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
ECHO [FAILURE] This script requires Administrator privileges.
PAUSE
GOTO :EOF
)

REM --- Step 2: Check and Start the Windows Time Service ---
SC query w32time | FIND "STATE" | FIND "RUNNING" > NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO [INFO] The Windows Time service is not running. Starting it...
NET START w32time
TIMEOUT /T 2 > NUL
)

REM --- Step 3: Configure, Update, and Resync ---
ECHO [INFO] Configuring and syncing with 'pool.ntp.org'...
w32tm /config /manualpeerlist:"pool.ntp.org" /syncfromflags:manual /reliable:yes > NUL
w32tm /config /update > NUL
w32tm /resync

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] The system clock has been successfully synchronized.
) ELSE (
ECHO [FAILURE] The clock synchronization failed.
ECHO Please check your network connection and firewall settings (UDP port 123).
)

ECHO.
PAUSE
ENDLOCAL

Conclusion

Using w32tm.exe is the standard and most reliable method for forcing a system clock synchronization from a batch script.

Key takeaways for a successful sync:

  • You must run the script as an Administrator.
  • Follow the three-step process: configure (/config /manualpeerlist), update (/config /update), and resync (/resync).
  • A robust script should also verify that the w32time service is running before sending commands.
  • Check the %ERRORLEVEL% after the w32tm /resync command to confirm the outcome of the operation.