≡ 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            


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            

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.

{ 4 comments… add one }
  • 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.

  • 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!

Leave a Comment