Getting Last Bootup Time

by Oct 5, 2015

In PowerShell 3.0 and better, it's trivial to get back real DateTime information from WMI using Get-CimInstance. This would tell you when your system was last booted:

#requires -Version 3 
(Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime 

In PowerShell 2.0, your only choice is Get-WmiObject which reports the information in internal WMI format:

(Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime 

Here, you would have to convert the WMI format manually:

$object = Get-WmiObject -Class Win32_OperatingSystem
$lastboot = $object.LastBootUpTime
$object.ConvertToDateTime($lastboot)

The ConvertToDateTime() conversion method is actually an attached method. Behind the scenes, it is a static method call that does the job:

$object = Get-WmiObject -Class Win32_OperatingSystem
$lastboot = $object.LastBootUpTime
[System.Management.ManagementDateTimeConverter]::ToDateTime($lastboot) 

Twitter This Tip! ReTweet this Tip!