<http://www.netikka.net/tsneti/info/tscmd169.htm>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Thu 7-Apr-2016 22:15:28

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



169} How to set the time server from cmd and how to synchronize the clock?

Caveats:
  1. It is assumed that the PC is not in a domain with a domain's own centralized time server.
  2. Some of the steps may be superfluous.
  3. You may have to "Run as administrator" at least on the post-XP Windows versions
@echo on & @setlocal enableextensions
@echo =========================
@echo Turn off the time service
net stop w32time
@echo ======================================================================
@echo Set the SNTP (Simple Network Time Protocol) source for the time server
net time /setsntp:time.nist.gov,0x1
@echo =============================================
@echo ... and then turn on the time service back on
net start w32time
@echo =============================================
@echo Tell the time sync service to use the changes
w32tm /config /update
@echo ===========================================
@echo Display the currently configured NTP server
net time /querysntp
@echo =======================================================
@echo Reset the local computer's time against the time server
w32tm /resync /rediscover
@endlocal & @goto :EOF

The output could be e.g.:
C:\_D\TEST>cmdfaq
=========================
Turn off the time service

C:\_D\TEST>net stop w32time
The Windows Time service is stopping.
The Windows Time service was stopped successfully.

======================================================================
Set the SNTP (Simple Network Time Protocol) source for the time server

C:\_D\TEST>net time /setsntp:time.nist.gov,0x1
The command completed successfully.

=============================================
... and then turn on the time service back on

C:\_D\TEST>net start w32time
The Windows Time service is starting.
The Windows Time service was started successfully.

=============================================
Tell the time sync service to use the changes

C:\_D\TEST>w32tm /config /update
The command completed successfully.
===========================================
Display the currently configured NTP server

C:\_D\TEST>net time /querysntp
The current SNTP value is: time.nist.gov,0x1

The command completed successfully.

=======================================================
Reset the local computer's time against the time server

C:\_D\TEST>w32tm /resync /rediscover
Sending resync command to local computer...
The command completed successfully.

Calling the Event Viewer ("eventvwr.msc /s") we can observe (this time having chosen time.windows.com)



If the Windows time service refuses to co-operate it may be necessary to give it a heftier wake-up call:
@echo off & setlocal enableextensions
net stop w32time
w32tm.exe /unregister
w32tm.exe /unregister
w32tm /register
net start w32time
endlocal & goto :EOF

The output could be e.g.
C:\_D\TEST>cmdfaq
The Windows Time service is stopping.
The Windows Time service was stopped successfully.

The following error occurred: Access is denied. (0x80070005)
W32Time successfully registered.
W32Time successfully registered.
The Windows Time service is starting.
The Windows Time service was started successfully.

An example of using an external program to set the time:

References/Comments:
  w32tm /?|more
  Configuring Windows 2000 and Windows XP to use NIST Time Servers
  How to synchronize the time with the Windows Time service in Windows XP
  Windows Time Service Tools and Settings
  Set the Windows clock from PC Annoyances By Steve Bass
  How do I use pool.ntp.org? NTP pool project
  Change Internet Time Update Interval
  Cmdtime 3, Softshape downloads
  Discussion: w32time gone missing (?)
  Discussion in alt.msdos.batch.nt
  how to sync windows time from a ntp time server in command
  NetTime - Network Time Synchronization Tool
  Make Windows synchronize time more often
  Change windows time synchronization interval?