Converting Those Weird WMI Dates and Times

Today's post is a follow-along. Fire up a copy of PowerShell (even v1 will do) and try this: Get-WmiObject -class Win32_OperatingSystem | Select-Object LastBootUpTime What the heck is that date and time all about? The problem with most WMI classes is that they store dates and times in a fairly hard-to-read form. Fortunately, PowerShell dynamically adds a couple of methods to ALL WMI classes, enabling you to convert between "Weird WMI Time" and "Normal Time."

Don Jones

November 29, 2010

2 Min Read
ITPro Today logo in a gray background | ITPro Today

Today's post is a follow-along. Fire up a copy of PowerShell (even v1 will do) and try this:

Get-WmiObject -class Win32_OperatingSystem | Select-Object LastBootUpTime

What the heck is that date and time all about? The problem with most WMI classes is that they store dates and times in a fairly hard-to-read form. Fortunately, PowerShell dynamically adds a couple of methods to ALL WMI classes, enabling you to convert between "Weird WMI Time" and "Normal Time." Here's an example:

Get-WmiObject -class Win32_OperatingSystem | Select-Object __SERVER,@{label='LastRestart';expression={$_.ConvertToDateTime($_.LastBootUpTime)}}

This makes use of the current WMI object's ConvertToDateTime() method, and asks it to convert the current object's LastBootUpTime property. This can be done with pretty much any WMI date and time, and it also demonstrates the trick of using a hashtable to create custom columns in your output. Do pay close attention to those (parentheses) and {curly braces}, as PowerShell is pickier than a fourth-grade English teacher when it comes to punctuation.

By the way, this is an example drawn right from my new instructor-led class - and you can enjoy five days of it this coming June in my hometown of Las Vegas. Current early bird pricing gets you five days of training and your hotel room and breakfasts and lunches and a gourmet dinner and a ton of other extras - for under $3,000! That pricing won't last long, though, so hop over to the Web site and check it out.
 
If you can't make the full class, I'll be offering workshops at various conferences in the Spring, and will be offering a 2-day workshop in both London and Germany in June. Be on the lookout for those details as they become available (they'll hit my Twitter feed, @concentrateddon, as well). 


A closing thought: The above hashtable trick with the custom output column also works with the Format-Table cmdlet. In v1 of PowerShell, Select-Object and Format-Table used a slightly different syntax to create custom properties/columns; in v2, Microsoft changed things so that both commands use either set of syntax. This is a really great technique to have in your utility belt, since it's got a ton of useful applications. 

Where might you use this trick? What WMI date and time properties do you find yourself dealing with?

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like