Monday, January 6, 2014

What is w32tm.exe in Windows?

If you are always using new computers or if you frequently upgrade machines, you will not face the issue of setting time each and every time you start computer. In my case, I am now using my old computer and its battery is in bad state to keep up the time when I turn off system.

Since I am using it mainly for browsing internet will be always there even though it is low speed. So decided not to replace the batter instead sync the time time with internet servers when ever I turn on the computer.Since I am using Win 7 I am able to do this by below steps

  1. Right click on the digital time shown in task manager and select 'Adjust date / time'
  2. In the opened dialog select 'Internet Time' tab
  3. Click on 'Change Settings'
  4.  Tick the "Synchronize with an Internet time server'
But this does syncing in a scheduled fashion. People says it differently such as one week and we can change by editing the registry etc..Somebody says the setting is only to make sure the time service is running not to correct the time.

But what I need is to set the time after I succeeds in dialing for internet. I can force the sync by using the 'Update now' button in the dialog.But it is too time consuming. So what should I do?

As per my belief, programmer should first write code to solve his problems before he code for others. So started digging for snippets which does this. This time cleverly didn't google for C#. Started with PowerShell as my aim was to have a simple script file which I can double click after connecting to internet. I got some snippets which does more like hitting the time server and updating the time. These are not the command which invokes the 'Update now'. As Windows is following the philosophy of command first approach, I was sure that I can find the corresponding command and I got after some more google. Its nothing but W32tm.exe located at <system drive>:\Windows\System32

Running w32tm.exe

When I simply run the command in the PowerShell window I got the below error

The following error occurred: The service has not been started. (0x80070426)

This is a good finding. Its looking for a service. Is it looking for a windows service in local machine or the internet time server service? I had disabled all the unfamiliar services in my machine as its too slow. After going thorough the services list, I would see one service names "Windows Time" which seems related to the above error message. I cannot enable it permanently as the machine is slow. So wrote in code itself.

net start w32time
w32tm.exe /resync
net stop w32time

When I run this ends with Access is denied.

It is little difficult to run the script in admin privilege in PowerShell. Either we need to have one file to start the actual file or we need to manually open cmd window as admin them execute the required script. So thought of moving top old favorite .bat script. Yes it worked

@echo off
net start w32time
w32tm.exe /resync
net stop w32time
pause

One time I got one issue which tells, cannot do sync as the time difference is big. In those situations just use the /force switch along with /resync. Explore the windows documentation about w32tm if you are interested.

Happy scripting.

No comments: