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.
Thank you for your article, it helped me.
This helped me too. I was struggling with that stupid date format.
Glad to hear that it is helpful
Very nice find! being able to call ([wmi]””) like that, really opens up a whole new side of powershell for me!
try CIM instead and it will return the date in a readable formay
Get-ciminstance win32_operatingsystem | select lastbootuptime