Getting time zone information in PowerShell is an exercise in simplicity. This single command:
[TimeZoneInfo]::Local
… yields about as much information as you’d generally care to know about the current system time zone.
Sample results:
Id : Mountain Standard Time DisplayName : (UTC-07:00) Mountain Time (US & Canada) StandardName : Mountain Standard Time DaylightName : Mountain Daylight Time BaseUtcOffset : -07:00:00 SupportsDaylightSavingTime : True
Setting the current time zone, unfortunately, isn’t quite as easy or intuitive. This is something I needed to do a while back for my desktop imaging process. Although the runtime portion of the Microsoft Deployment Tools (MDT) are largely built on a VBScript foundation, most of my deployment scripts are PowerShell-based, so I came up with a simple Posh function for setting the system time zone that works for both Windows XP and Windows 7.
There are a number of crazy ways to set time zone information, but rather than delving into methods that directly modify the registry and such, I decided upon documented, supported ways to change the current time zone:
function Set-TimeZone { param( [parameter(Mandatory=$true)] [string]$TimeZone ) $osVersion = (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue("CurrentVersion") $proc = New-Object System.Diagnostics.Process $proc.StartInfo.WindowStyle = "Hidden" if ($osVersion -ge 6.0) { # OS is newer than XP $proc.StartInfo.FileName = "tzutil.exe" $proc.StartInfo.Arguments = "/s `"$TimeZone`"" } else { # XP or earlier $proc.StartInfo.FileName = $env:comspec $proc.StartInfo.Arguments = "/c start /min control.exe TIMEDATE.CPL,,/z $TimeZone" } $proc.Start() | Out-Null }
You can call this function using the friendly time zone name associated with the zone you want. For example,
Set-TimeZone "Pacific Standard Time" Set-TimeZone "North Asia East Standard Time" Set-TimeZone "GMT Standard Time"
… and so on.
Note that the function uses the .NET Process class to execute the necessary commands. The Windows 7 tzutil method doesn’t need this, but the control.exe command used for XP and earlier won’t work with the Start-Process cmdlet, so I went with the lowest common denominator.
Recent Comments