3
PowerShell Script: Convert To Local Time From UTC
Posted by Tao Yang on 11/01/2012 in PowerShell | ∞
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:
Function Get-LocalTime($UTCTime)
{
$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$TZ = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone)
$LocalTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($UTCTime, $TZ)
Return $LocalTime
}
... other posts by Tao Yang
Tags: Powershell
- Subscribe via RSS
Featured
Archives
- June 2013 (1)
- May 2013 (2)
- April 2013 (5)
- March 2013 (7)
- February 2013 (1)
- January 2013 (3)
- December 2012 (2)
- November 2012 (2)
- October 2012 (4)
- September 2012 (4)
- August 2012 (6)
- July 2012 (1)
- June 2012 (1)
- May 2012 (4)
- April 2012 (4)
- March 2012 (4)
- February 2012 (5)
- January 2012 (9)
- December 2011 (1)
- November 2011 (1)
- October 2011 (5)
- September 2011 (1)
- August 2011 (4)
- July 2011 (2)
- June 2011 (3)
- May 2011 (7)
- April 2011 (2)
- March 2011 (1)
- February 2011 (2)
- January 2011 (3)
- November 2010 (3)
- September 2010 (3)
- August 2010 (3)
- July 2010 (9)
- June 2010 (3)
Categories
- Active Directory (1)
- DNS (1)
- Hyper-V (4)
- Microsoft Exchange (1)
- Others (9)
- PowerShell (44)
- SC Orchestrator (1)
- SCCM (31)
- SCDPM (1)
- SCOM (56)
- SCSM (2)
- SCVMM (3)
- SQL Server (1)
- VBScript (1)
- Windows (5)
Tags
70-400 Alerts Console Crash Daylight Saving Email Notifications Emails Exams Exchange 2007 Exchange 2010 Failover Management Servers Featured Hardware Inventory Health Check Hyper-V Inventory Management Pack MOM Morning Check MP Authoring NTFS Permission Others Powershell Powershell Remoting Primary Management Server remote execution policy SCCM SCCM 2012 SCCM Reports SCOM SCOM agent assignments SCOM Management Pack SCOM Migration SCOM Notifications SCOM Reporting SCVMM Secondary Site SMTP SQL Stopping MOM Agents TMG URL Encoding VBScript Web Console Windows Windows Scheduled TasksRecommended Blogs
Recommended Sites
Recent Comments
- OpsMgr: Self Maintenance Management Pack - Operations Manager - SCOM on OpsMgr Self Maintenance Management Pack
- Vinz on ENHANCED SCOM Alerts Notification Emails!
- Steve on PowerShell Script To Enable SCOM Agent Proxy in a More Efficient Way
- Tao Yang on OpsMgr Self Maintenance Management Pack
- Jamie on OpsMgr Self Maintenance Management Pack
3 Comments
I was working on adding AlertHistory to your Enhanced Email script (as I’ve not found it to be implemented anywhere after searching around Google for a while) and wanted to format the timestamps into server local time instead of UTC and this script is perfect!
Thanks again, Tao!
~Tyson
Thanks so much! This is exactly what i need…
SCOM task result contains UTC time and I need to convert it to local time.
BTW, When i google something, i am always redirected to you ,LOL
Or you can just use the .NET method provided
$UTC.ToLocalTime()
If you want another time zone that is not local to your computer, use the DateTimeOffset object.