Powershell script that would change the W32time parameters in the registry for multiple windows servers

Powershell script that would change the W32time parameters on multiple servers.

Location of parameters: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\Parameters

1. Pull a list of windows servers from a .txt file
2. Change the "NTPServer" string to: time.windows.com,0x9
3. Change the "Type" string to:  NT5DS
4. run: net stop w32time
5. run: net start w32time
6. run: w32tm /resync
7. Export results to a .csv file  (If possible)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try this:
$Result = @()
Get-Content -Path C:\Temp\test.txt |
	ForEach-Object {
		"Processing $_ ... " | Write-Host -Foregroundcolor White -NoNewline
		$ResultConfig = $Null
		$ResultResync = $Null
		$Output = "Offline"
		If (Test-Connection -ComputerName $_ -Count 2 -Quiet) {
			$Output = & w32tm.exe /config /computer:$_ /update /syncfromflags:domhier
			If ($LASTEXITCODE -eq 0) {
				$ResultConfig = "OK"
				"config OK " | Write-Host -Foregroundcolor Green -NoNewline
				"... " | Write-Host -Foregroundcolor White -NoNewline
				$Output = & w32tm.exe /resync /computer:$_ /rediscover
				If ($LASTEXITCODE -eq 0) {
					$ResultResync = "OK"
					"resync OK." | Write-Host -Foregroundcolor Green
				} Else {
					$ResultResync = $Output
			} Else {
				$ResultConfig = $Output
		} Else {
			$ResultConfig = $Output
		If (($ResultConfig -ne "OK") -Or ($ResultResync -ne "OK")) {
			$Output -join " " | Write-Host -Foregroundcolor Red
		$Result += $_ | Select-Object -Property `
			@{Name="Computername"; Expression={$_}},
			@{Name="Config"; Expression={$ResultConfig}},
			@{Name="Resync"; Expression={$ResultResync}}

Open in new window

As to the script content: there's no need for registry editing, this can all be done using w32tm.exe.
2.: Not required; if the type is NT5DS (domain hierarchy), the ntp server entry is not used.
4.-5.: Not required with "w32tm /update"
7.: The script will generate a custom object that you can treat however you want, especially using something like
.\Whatever.ps1 | Export-Csv -Path "C:\Temp\w32tm.csv" -NoTypeInformation

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mjm21Author Commented:
Nice job!  Worked perfectly!!  So in the text file OK to place servers all in a column?


I only tested this on one.
Yes, just like that.
mjm21Author Commented:
Excellent job!  Many thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows OS

From novice to tech pro — start learning today.