≡ Menu

Powershell: Working with WMI date time format

Often I have seen people struggling to convert date time value that is returned from a WMI class. For example, below query returns the timestamp of the time computer booted last time but it is not a readable format.

(Get-WmiObject -Class Win32_OperatingSystem).LastBootupTime            

Output:            
20110811122050.894035+330

Here you have multiple ways to convert this raw data to some readable format. You can manually parse the raw text using conventional methods but that is not efficient. The procedure I recommend is using the WMI built-in methods.

Let us see how to do that.

$boottime = Get-WmiObject win32_Operatingsystem            
$boottime.ConvertToDateTime($boottime.LastBootUpTime)

As you can see in the above code, I am using ConvertToDateTime method that is available in WMI itself to convert the datetime from WMI format to very conventional date time format which humans can read easily. We are just passing the raw value of LastBootupTime to the function. Isn’t it looking very easy instead of parsing the output and converting into some meaningful format? But you hate to use two lines to convert the date and looking for even single liner? Here you go …

([wmi]"").ConvertToDateTime((Get-WmiObject -Class Win32_OperatingSystem).LastBootuptime)

Much simpler. Isn’t it?

Hope this tip helps you and saves you some time.

Comments on this entry are closed.

  • vik April 14, 2015, 9:08 am

    Thank you for your article, it helped me.

  • Matt Williams September 17, 2016, 12:34 am

    This helped me too. I was struggling with that stupid date format.

    • Wintel Rocks September 24, 2016, 10:09 am

      Glad to hear that it is helpful

  • J October 31, 2016, 4:58 pm

    Very nice find! being able to call ([wmi]””) like that, really opens up a whole new side of powershell for me!

  • John Garrabrandt March 1, 2018, 3:28 am

    try CIM instead and it will return the date in a readable formay
    Get-ciminstance win32_operatingsystem | select lastbootuptime