How to find NTP Server using PowerShell?

Hello, PowerShell enthusiast today I will be sharing a script to fetch the NTP (Network Time Protocol) server name from remote servers using our favourite PowerShell. We as windows admin know that a Primary Domain controller holds the NTP role. If you are not aware of the term then let’s understand what is NTP before proceeding ahead.

What is NTP server and how to check it?

The default time synchronization protocol used by the Windows Time service in the operating system is Network Time Protocol (NTP). NTP is a fault-tolerant, highly scalable time protocol that is most commonly used to synchronize computer clocks using a known time reference.

You can check the NTP source by using the below command on the server or your personal computers as well in the command prompt or in PowerShell. If the command gives Local CMOS clock as an output then your server/system is taking time from the CMOS battery or the ESXI. If the output is any time source then it might be taken from the internet or some specified NTP source.

				
					w32tm /query /source
				
			

Finding NTP server using PowerShell

Step #1 

New-Object System.Collections.ArrayList is used to collect data and unlike array it is not fixed and of 
course,we have a Foreach loop to loop through the provided list of the server.
				
					$Inventory = New-Object System.Collections.ArrayList
$AllComputers = gc C:\users\admin\desktop\server.txt
foreach($computers in $allcomputers){
$Computers
				
			

Step #2

 $ComputerInfo will create a new object of system object ( It is being used to dump the output to CSV file).

 

$ntp this is our main command, it will search for the source on each of the mentioned servers provided in the list.

				
					$ComputerInfo = New-Object System.Object
$ntp = w32tm /query /computer:$computers /source

				
			

Step #3

Here the type ServerName is filled with the values of $computers and type NTP Source is filled with $ntp values. Each of them is stored into $computerinfo.

 

				
					$ComputerInfo |Add-Member -MemberType NoteProperty -Name "ServerName" -Value "$Computers" 

$ComputerInfo |Add-Member -MemberType NoteProperty -Name "NTP Source" -Value "$Ntp" 

				
			

Step #4

All the values stored in $ComputerInfo are added to $Inventory.

 

Out-Null is being used to give no output on the screen.

 

				
					$Inventory.Add($ComputerInfo) | Out-Null
  }

$Inventory | Export-Csv C:\users\admin\desktop\NTP.csv -NoTypeInformation
				
			

Result of the PowerShell script

Below is the result of the script in the CSV format.

 

ServerNameNTP Source
MY-PCtime.windows.com,0x9 
test-server1dc.test.com
test-server2dc.test.com

 

Conclusion

I hope you have liked the post How to find NTP Server using PowerShell? and will implement this whenever it is required. Things you should always remember is to try with one server so that it will be easy for you to make changes. Let me know if you want a blog post on some other script that might amaze you.

We are working continuously to provide you with the better and the best scripts daily. We will publish weekly hence don’t forget to subscribe to our newsletter. 

Related Posts

Leave a Reply

Please disable your adblocker or whitelist this site!