PowerShell Script: Convert To Local Time From UTC

I wrote this function in the script from my previous post “SCOM MP Authoring Example: Generate alerts based on entries from SQL Database (Part 2 of 2)”. It comes handy sometimes so I thought I’ll blog it separately as well. In PowerShell Datetime object, there is a ToUniversalTime() method that converts local time to UTC time. However, there isn’t a native way to convert FROM UTC To local time. So I wrote this function: [sourcecode language=”powershell”] Function Get-LocalTime($UTCTime) { $strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName $TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone) $LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ) Return $LocalTime } [/sourcecode]

Continue reading
%d bloggers like this: