2

Essentially, I want to get an accurate time from a public NTP server, in DOS or powershell.
I know there's the w32tm which can query a remote ntp server, but the result it gives is how much out my computer time is, not the actual time of the remote ntp server.

I've found various windows apps that work exactly as I want, but i can't get the output to a variable to do anything with. I'm dead surprised there's no .exe that just gets the time from a source, or that it wasn't written into w32tm.

Something like ntpquery.exe pool.ntp.org (followed maybe like a timezone or something) with an output of the date / time is all I'm after.


I know there's a command on another question that shows this result:

w32tm /stripchart /computer:foo
Tracking foo [1.2.3.4].
The current time is 19/07/2012 17:39:48 (local time).
17:39:48 d:-00.0000840s o:-25.3004160s  [@                          |                           ]

This shows the local system time (which could and in my case is wrong) and how much out it is to the NTP server (example above is 0.00000840s)

If I run this I could get a result of +/- 3000 seconds or even more, which doesn't give me the actual time of the NTP server just tells me how out I am.


What I'm looking for is a way to get the time from the NTP source in DOS / powershell so I can use it as a variable.

6
  • 5
    Does this answer your question? How can I query an NTP server under Windows? Commented Aug 7, 2020 at 21:05
  • 1
    What does this have to do with the MS-DOS operating system?
    – Ramhound
    Commented Aug 7, 2020 at 22:15
  • 2
    Please see this script on TechNet. You can then include this into your powershell script and use it like this: $NTPTime = (Get-NtpTime).NtpTime. This will store the time returned from the default ntp server (pool.ntp.org) in a DateTime data type. You can change which NTP server by using the -server parameter.
    – CraftyB
    Commented Aug 9, 2020 at 9:57
  • @spikey_richie the other question is a general Windows one, while this one is about doing it in cmd/powershell. Also the other question's only cmd answer uses w32tm and OP is looking for another solution. I'd say not a duplicate.
    – Gnoupi
    Commented Aug 10, 2020 at 16:46
  • 1
    @CraftyB if anyone is looking for this script in the future, the Wayback Machine has it here and it can be found on PS Gallery here.
    – reeeky2001
    Commented Nov 1, 2023 at 13:12

1 Answer 1

0

Thanks for all the responses... esp CraftyB.... though I figured out a way to do it in the end and thought I'd share with.

What I did was use this app here (free) https://www.meinbergglobal.com/english/sw/ntp.htm Installed.. then took the libcrypto-1_1.dll and ntpdate.exe from the installation and scripted that to get the result from a remote ntp server.

Works fab. Thanks so much for everyone's input - and CraftyB your response is a valid answer that also works.. so I may migrate to that in the end !

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .