Skip to content

Instantly share code, notes, and snippets.

@takato3000
Last active September 29, 2022 03:55
Show Gist options
  • Select an option

  • Save takato3000/33dcbcd4404d7d25ea44f082cf4d5ba5 to your computer and use it in GitHub Desktop.

Select an option

Save takato3000/33dcbcd4404d7d25ea44f082cf4d5ba5 to your computer and use it in GitHub Desktop.
windows ntp powershell script
function Set-TimeSynchronization {
param(
[string[]] $TimeSource = 'time.windows.com',
[int] $MaxPosPhaseCorrection = 5,
[int] $MaxnegPhaseCorrection = 5,
[int] $PollInterval = 1800
)
## set external time source
## set server type to NTP
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters -Name Type -Value 'NTP'
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config -Name AnnounceFlags -Value 5
## Enable NTP server
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer -Name Enabled -Value 1
## Specify Time source
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Parameters -Name NtpServer -Value "$TimeSource,0x1"
## Set poll interval in seconds - every 30 minutes
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient -Name SpecialPollInterval -Value $PollInterval
## set max +/- time corrections in seconds - 5 seconds
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config -Name MaxPosPhaseCorrection -Value $MaxPosPhaseCorrection
Set-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\W32Time\Config -Name MaxnegPhaseCorrection -Value $MaxnegPhaseCorrection
Stop-Service -Name W32Time
Start-Service -Name W32Time
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment