Search code examples
windowsbatch-filetimecmdsynchronization

How to automatically sync Date & Time in windows?


I would like to write a .bat file that I can schedule with Task Scheduler in Windows. The script should open the time settings and automatically click the sync option like so:

enter image description here

So far I was able to open this window by using the following line of code:

start ms-settings:dateandtime

However I do not know how to click on the Sync now button.

Do you know a smart and elegant way to achieve this task? Feel free to suggest a completely different approach if the final result is the same.


Solution

  • w32tm /resync Must be run by the administrator or else you get a denied message

    For more details on other options see https://learn.microsoft.com/en-us/windows-server/networking/windows-time-service/windows-time-service-tools-and-settings

    w32tm /resync
    Sending resync command to local computer
    The following error occurred: Access is denied. (0x80070005)
    

    enter image description here

    On a domain then use

    w32tm /domain
    

    want to use a different server than default ? then replace time.server.com

    w32tm /config /update /manualpeerlist:time.server.com
    

    enter image description here

    see https://www.ntppool.org/en/use.html