I found myself with a list of UTC timestamps and wanted to convert them into the local time zone. Using this web post as a starting point, I modified the script to grab a text file containing UTC times (one per line) and loop over each one outputting the results in a nice two column format.
Is there a more efficient method for accomplishing this? I've seen people mention the .NET
method $UTC.ToLocalTime()
but I didn't find details on that, so I went with my frankenstein script.
$UTCTime = GC "C:\Scripts\UTC.txt"
$results = '' | Select UTCTime,PST
Foreach ($newtime in $UTCTime){
$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone)
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($newtime, $TZ)
$results.UTCTime = $newtime
$results.PST = $LocalTime
$results
}
Input is: (and powershell might be flexible enough to handle various input formatting, more testing is needed to determine this)
4/19/2015 4:10:03 AM
4/19/2015 9:10:03 AM
Output is:
UTCTime PST ------- --- 4/19/2015 4:10:03 AM 4/18/2015 9:10:03 PM 4/19/2015 9:10:03 AM 4/19/2015 2:10:03 AM