There is an excellent post on getting and setting the TimeZone done by Pat Richard using Powershell and TZUtil which I thought was absolutely cool.

The only thing I noticed was the Script had a defined list for the Time Zones.   Knowing much of this information is contained directly in Windows I was wondering if there was a way to pull the list in and use it as the master.

Within .NET there is System.TimeZoneInfo which has a nice simple method called (are you ready?) GetSystemTimeZones

So to leverage this method in Windows Powershell all we need to do it

[System.TimeZoneInfo]::GetSystemTimeZones()

Poof! now we have a big funky list on our screen! But now to make it more useful as no ITPro likes to keep typing in .NET (Well some of us do, but we’re more classified as half dev/half itpro hobbits)

So we can make this a nice simple advanced Function

function get-timezone
{
[cmdletbinding()]
param([string]$Name)
([system.timezoneinfo]::GetSystemTimeZones() | where { $-.ID –like "*$Name*” }) 
}

or possibly a Function to only pull up a matching one?

function get-timezonematch
{
[cmdletbinding()]
param([string]$Name)
if ([system.timezoneinfo]::GetSystemTimeZones() | where { $-.ID -eq $Name })
{ Return $TRUE }
else
{ Return $FALSE }
 
}

Now I can do something like THIS

GET-TIMEZONE | Format-table –autosize

or

GET-TIMEZONE *EAST*

or

GET-TIMEZONEMATCH “Eastern Standard Time”

Now we can take Pat’s original script and give it a slight improvement to pull from the Internal list from Windows.  If we have already defined the “GET-TimeZone” Advanced function and executed it first, we can do this Pat’s function.

function Set-TimeZone {
[CmdletBinding(SupportsShouldProcess = $True)]
param(
    [Parameter(ValueFromPipeline = $False, ValueFromPipelineByPropertyName = $True, Mandatory = $False)]
    [ValidateNotNullOrEmpty()]
    [string]$TimeZone = "Eastern Standard Time"
  )

If (GET-TIMEZONEMATCH $TimeZone) {  
  $process = New-Object System.Diagnostics.Process
  $process.StartInfo.WindowStyle = "Hidden"
  $process.StartInfo.FileName = "tzutil.exe"
  $process.StartInfo.Arguments = "/s `"$TimeZone`""
  $process.Start() | Out-Null }

ELSE { WRITE-ERROR “InvalidTimeZone” }

} # end function Set-TimeZone

Pretty cool what you can do with Powershell and .NET and STILL be classified as an ITPro.  Also Thanks to Pat Richard, Lync MVP Extraordinaire for writing this cool Powershell Script Smile