Exploring Get-TimeZone in PowerShell
Welcome back to Wahmans PowerShell blog! Today we’re diving into a handy cmdlet that helps you work with time zones in Windows PowerShell: Get-TimeZone. Whether you’re scripting for automation or building time-aware applications, understanding this cmdlet is essential. According to Microsoft, Get-TimeZone “gets the current time zone or a list of available time zones.” Let’s explore what this means in practice and walk through some examples, from beginner to advanced usage.
Basic Usage of Get-TimeZone
When run without parameters, Get-TimeZone returns the current time zone of the system.
Get-TimeZone
This command will output something like:
Id : Central European Standard Time
DisplayName : (UTC+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna
StandardName : Central European Standard Time
DaylightName : Central European Summer Time
SupportsDaylightSavingTime : True
Example 1: List All Available Time Zones
To see what time zones are available for use, simply add the -ListAvailable switch:
Get-TimeZone -ListAvailable
This is particularly useful when you want to convert or set time zones and need a reference for valid time zone IDs.
Example 2: Convert DateTime to Another Time Zone
You can convert a specific DateTime to a different time zone using the [System.TimeZoneInfo] .NET class combined with Get-TimeZone.
$sourceTime = Get-Date
$destinationTZ = [System.TimeZoneInfo]::FindSystemTimeZoneById("Pacific Standard Time")
$convertedTime = [System.TimeZoneInfo]::ConvertTime($sourceTime, $destinationTZ)
$convertedTime
This snippet shows you how to convert the current system time to Pacific Standard Time.
Example 3: Schedule a Task at a Different Time Zone
Suppose you’re running an automated job that needs to start at 9 AM PST regardless of the machine’s own time zone. You can use Get-TimeZone and conversions to make sure the task triggers properly:
$localTimeNow = Get-Date
$taskTimeInPST = Get-Date -Hour 9 -Minute 0 -Second 0
$localTZ = Get-TimeZone
$targetTZ = [System.TimeZoneInfo]::FindSystemTimeZoneById("Pacific Standard Time")
$convertedTime = [System.TimeZoneInfo]::ConvertTime($taskTimeInPST, $targetTZ, $localTZ)
$convertedTime
This ensures your script always calculates the local equivalent of 9 AM PST.
Example 4: Dynamic Time Zone Change (Advanced)
If you’re writing a script that needs to temporarily change the system time zone, use Set-TimeZone in combination with Get-TimeZone (note: admin privileges required):
$originalTZ = Get-TimeZone
Set-TimeZone -Id "UTC"
# Do some UTC-specific operations
Start-Sleep -Seconds 10
Set-TimeZone -Id $originalTZ.Id
This pattern helps maintain system integrity by restoring the original time zone after the task completes.
In Summary
The Get-TimeZone cmdlet is more than just a tool for checking your system clockâit’s a foundational piece in localizing scripts, managing global tasks, and working with time-aware applications.
Happy scripting, and I will see you in the next post!
Leave a Reply