Skip to content

Instantly share code, notes, and snippets.

@auberginehill
Last active April 10, 2019 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auberginehill/299857bdf861cc70578fbef699c458a3 to your computer and use it in GitHub Desktop.
Save auberginehill/299857bdf861cc70578fbef699c458a3 to your computer and use it in GitHub Desktop.
Syncs Windows Time from an External Time Source (a Windows PowerShell script).
<#
Sync-WindowsTime.ps1
#>
# Requires administrator rights
$empty_line = ""
# Check if the PowerShell session is elevated (has been run as an administrator)
# Credit: alejandro5042: "How to run exe with/without elevated privileges from PowerShell" http://stackoverflow.com/questions/29266622/how-to-run-exe-with-without-elevated-privileges-from-powershell?rq=1
If (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator") -eq $false) {
$empty_line | Out-String
Write-Warning "It seems that this script is run in a 'normal' PowerShell window."
$empty_line | Out-String
Write-Verbose "Please consider running this script in an elevated (administrator-level) PowerShell window." -Verbose
$empty_line | Out-String
$admin_text = "For performing system altering procedures, such as removing scheduled tasks, disabling services or writing the registry the elevated rights are mandatory. An elevated PowerShell session can, for example, be initiated by starting PowerShell with the 'run as an administrator' option."
Write-Output $admin_text
$empty_line | Out-String
"Exiting without making any changes to the system." | Out-String
Return ""
} Else {
$continue = $true
} # Else
# Check if the computer is connected to the Internet
# Credit: ps1 "Test Internet connection": http://powershell.com/cs/blogs/tips/archive/2011/05/04/test-internet-connection.aspx
$empty_line | Out-String
If (([Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]'{DCB00C01-570F-4A9B-8D69-199FDBA5723B}')).IsConnectedToInternet) -eq $false) {
"The Internet connection doesn't seem to be working. Exiting without syncing the time." | Out-String
Return ""
} Else {
$continue = $true
} # Else
# Set the Windows Time sync parameters
# Credit: Adrian K: "How to Sync Time from External Time Source?" http://adriank.org/windows-server-2008-r2-sp1-sync-time-external-time-source/
# W32tm /query /configuration
W32tm /config /manualpeerlist:"time.windows.com,0x1" /syncfromflags:MANUAL /reliable:YES /update
W32tm /config /update
Stop-Service -Name "W32Time"
Start-Service -Name "W32Time"
# Sync Windows Time from an External Time Source
W32tm /resync
$empty_line | Out-String
# Credit: Adrian K: "How to Sync Time from External Time Source?" http://adriank.org/windows-server-2008-r2-sp1-sync-time-external-time-source/
# Credit: alejandro5042: "How to run exe with/without elevated privileges from PowerShell" http://stackoverflow.com/questions/29266622/how-to-run-exe-with-without-elevated-privileges-from-powershell?rq=1
# Credit: ps1 "Test Internet connection": http://powershell.com/cs/blogs/tips/archive/2011/05/04/test-internet-connection.aspx
# Source: http://support.microsoft.com/kb/816042 (about the 0x1 flag)
# Source: https://docs.microsoft.com/en-us/windows-server/networking/windows-time-service/windows-time-service-tools-and-settings
# Home: https://gist.github.com/auberginehill/299857bdf861cc70578fbef699c458a3
@auberginehill
Copy link
Author

[Reserved]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment