Friendly WMI Dates

Gee..you think you know something only to find out you don’t. Or maybe this falls into the category of teaching an old dog new tricks.

When I first started using PowerShell several years ago, I learned about how to convert a WMI date to a more user friendly format. For example, this is not especially easy to read:

PS C:\> (gwmi win32_operatingsystem).lastbootuptime
20090804133238.086379-240

However, all WMI objects in PowerShell have a ConvertToDateTime() method. I simply got in the habit of using Select-Object to define a custom property with the re-formatted date.

PS C:\> gwmi win32_operatingsystem | Select Caption,@{name="LastBoot";
>> Expression={$_.ConvertToDateTime($_.LastBootUpTime)}}
>>

Caption                                                     LastBoot
——-                                                     ——–
Microsoft Windows XP Professional                           8/4/2009 1:32:38 PM

There’s nothing technically wrong with this approach. This method was added so that administrators who knew nothing about .NET, like myself at the time, would have a relative easy way of converting WMI datetime strings. But once you learn more PowerShell you discover where this method comes from.

In the types.ps1xml file, you will find the ConvertToDateTime() method defined for all WMI objects.

<Name>ConvertToDateTime</Name>
    [System.Management.ManagementDateTimeConverter]::ToDateTime($args[0])

The method is simply calling the System.Management.ManagementDateTimeConverter objects ToDateTIme() method. Once you realize this my original expression can also be written:

PS C:\> gwmi win32_operatingsystem | Select Caption,@{name="LastBoot";
>> Expression={[System.Management.ManagementDateTimeConverter]::ToDateTime(
>> $_.LastBootUpTime)
>> }}
>>

Caption                                                     LastBoot
——-                                                     ——–
Microsoft Windows XP Professional                           8/4/2009 1:32:38 PM

Whether that is easier to type or understand may be a matter of personal preference. However if I am presented with a WMI datetime string, perhaps from another object or maybe a log file, I can now easily convert it.

PS C:\> $x="20090708102530.360000-240"
PS C:\> [system.management.managementDateTimeConverter]::ToDateTime($x)

Wednesday, July 08, 2009 10:25:30 AM

So if you have some old habits, now may be the time to check them and discover if perhaps there is a better or different way to accomplish something, especially with the impending arrival of PowerShell v2.0.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to StumbleUpon

This entry was posted in PowerShell, Scripting, WMI and tagged , , , , . Bookmark the permalink.

2 Responses to Friendly WMI Dates

  1. dboftlp says:

    Hey Jeff,
    I’m working with event logs and have an export-csv Q? What I have so far is remote collection of system and application logs for the last week via
    #code
    TimeGenerated >= ‘$((get-date).AddDays(-7).toShortDateString())’
    #code
    I’ve selected LogFile, EventCode, RecordNumber, TimeGenerated, Type, and several others before I pipe it to export-csv. When it exports, the TimeGenerated field is in that weird format:
    20090820195735.000000-240
    and I would like it more readable. I know it converts the info for the comparison of those logs, but how do I keep that format for the export?

    TIA
    dboftlp

    • Jeffery Hicks says:

      In the Select part of your pipelined expression, a hash table like I did in the post and convert the TimeGenerated value.

      …. | Select Logfile, Eventcode,RecordNumber,@{name=”TimeGenerated”;Expression={$_.ConvertToDateTime($_.TimeGenerated}},Type | Export-Csv …