Synchronize Internet Time in a Windows script?

11

3

Is there a way I can synchronize the system clock with an Internet Time server (i.e: time.nist.gov) via command-line? Preferably, this should be something executable via BAT or PowerShell script.

This particular need arises because I have some VMs for which I regularly use the "save machine state" function, and these don't typically re-synchronize their clocks when I "wake" them. Often times, I'll re-load them to find the clock is (and is staying) days out-of-synch.

Re-synching the clock through the Internet Time Settings dialog usually works fine, but it takes a lot of mouse-clicking to do this. I'd much rather just keep a script on the desktop that I can double-click and have the job done for me.

Right now this is for a system running Windows 7 Home x64, but backward-compatibility to XP SP3 would be nice to have.

Iszi

Posted 2011-08-15T20:35:37.003

Reputation: 11 686

1Most VM software (Virtual PC, VirtualBox) comes with drivers ("Additions") that keep the guest's clock in sync with host, among other things. – user1686 – 2011-08-15T20:46:36.227

@grawity - Using those, but it doesn't seem to do the job. – Iszi – 2011-08-15T21:08:48.953

Answers

13

See the reference for W32tm, "A tool used to diagnose problems occurring with Windows Time": http://technet.microsoft.com/en-us/library/bb491016.aspx. Also, w32tm /?

Use w32tm /resync. You may also need to set a server with net time if you haven't done that yet.

Jeff Ferland

Posted 2011-08-15T20:35:37.003

Reputation: 823

@echo off && w32tm/resync - Command propmpt says that it does not recognize command (w32tm) – Arpit Parekh – 2014-10-03T18:01:31.910

.. The command I could not run on windows 7. – Arpit Parekh – 2014-10-03T18:12:02.197

@Iszi My mistake, actually I just have to select command prompt as Run as administrator. The command is working very fine. Thanks – Arpit Parekh – 2014-10-03T18:43:21.470

I have a mistake: "Sending resync command to local computer. The computer did not resync because the required time change was too big. Press any key to continue..." What should I do? – stan – 2014-11-24T20:52:54.733

@Iszi I suggest removing one &, namely @echo off && w32tm /resync & PAUSE, so that the command window remains open also in case of errors. – Antonio – 2016-03-25T06:25:33.053

See also http://stackoverflow.com/a/13811519/2436175 to know how to run as administrator

– Antonio – 2016-03-25T06:36:48.730

If you get "the service has not been started" error, check your services and make sure the Windows Time service is running (and starts automatically). – Kirk Liemohn – 2016-05-20T16:38:53.313

@KirkLiemohn I was getting the same error that the service has not been started and thanks for your tip, I opened Task Manager > Services tab > found W32Time > right clicked and started it.

Any idea why it doesn't start itself when the Computer is turned on? How do I set it to start it automatically? – Deepak Kamat – 2017-05-29T14:58:14.243

1@DeepakKamat, you probably need to use Windows Services (type "services.msc" in a run or search box). Scroll down to find Windows Time. Right click on it and choose Properties. There you can set the startup type to be automatic. – Kirk Liemohn – 2017-05-30T15:50:03.977

2Okay, I did... @echo off && w32tm /resync && PAUSE Threw that in a BAT file, and set a shortcut on the desktop with "Run as administrator" checked. Thanks! – Iszi – 2011-08-15T21:09:55.273

On Windows 7, when I do this I get: The following error occurred: The service has not been started. – User – 2013-02-12T00:33:55.023

1

@echo off
net start W32Time
w32tm /resync
echo sync complete
  1. Copy this code to Notepad and save it as a .bat file.
  2. Run the batch file with Administrator privileges or schedule a task to run this batch file from the Task Scheduler, as required.

Note: This required Administrator privileges and a working Internet connection.

Sainath Prabhu

Posted 2011-08-15T20:35:37.003

Reputation: 11

0

I used to do it with the net command.

for ntp

net time ntp.server.foo

for windows

net time \\server

I can't recall if you needed some sort of /ntp flag for the ntp... it will tell you if you do.

Kyle__

Posted 2011-08-15T20:35:37.003

Reputation: 624