Get System Last Boot Up Time

This script uses WMI to get a system last boot up time. It accepts input from the pipeline.

 
 
 
 
 
5 Star
(1)
Add to favorites
Operating System
5/8/2010
E-mail Twitter del.icio.us Digg Facebook
  • Datetime format
    5 Posts | Last post August 25, 2011
    • the [datetime] convert line is wrong.
      
      This one works.
      [System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class Win32_OperatingSystem -ComputerName Localhost).lastBootUpTime)
    • Here is my fix for the issue. It also includes a time format addition.
      $LastBootUpTime = (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName).LastBootUpTime
      $Report.LastBootUpTime = [datetime]::ParseExact($LastBootUpTime.split(".")[0],'yyyyMMddHHmmss',$null)
      
    • Here is the error (from PoSH V2) from the original code posted by Robert :
      
      Cannot convert value "20101120031927.782901-360" to type "System.DateTime". Error: "String was not recognized as a v
      d DateTime."
      At C:\ps\Get-SystemLastBootUpTime.ps1:38 char:118
                $Report.LastBootUpTime = [datetime] (Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerNam
       <<<< LastBootUpTime
            CategoryInfo          : NotSpecified: (:) [], RuntimeException
            FullyQualifiedErrorId : RuntimeException
    • And Paul's mod works.
    • another optional fix:
      $Report.LastBootUpTime = [datetime](Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName).ConvertToDateTime((Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName).LastBootUpTime)