Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friendly WMI Dates

Posted on August 5, 2009August 5, 2009

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

2 thoughts on “Friendly WMI Dates”

  1. dboftlp says:
    August 24, 2009 at 4:32 pm

    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

    1. Jeffery Hicks says:
      August 25, 2009 at 7:41 am

      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 …

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d