header image

System Up Time

Posted by: | August 12, 2009 | 3 Comments |

The PowerShell team have just posted about obtaining system up time using a .NET class to convert the WMI date format to something thats readable


There is a WMI only way of doing this

$os = Get-WmiObject -Class Win32_OperatingSystem

But this gives the date and time the system was last booted.  If you want the actual up time then use

(get-date) – $os.ConvertToDateTime($os.LastBootUpTime)

which generates a timespan object

Days              : 0
Hours             : 1
Minutes           : 53
Seconds           : 8
Milliseconds      : 461
Ticks             : 67884618000
TotalDays         : 0.0785701597222222
TotalHours        : 1.88568383333333
TotalMinutes      : 113.14103
TotalSeconds      : 6788.4618
TotalMilliseconds : 6788461.8

which gives the actual up time.  The .NET method can be substituted for the WMI method if required

Technorati Tags: ,,
under: PowerShell and WMI


  1. By: pan_2@LJ on August 13, 2009 at 6:01 am      

    (get-date) – (Get-WmiObject Win32_OperatingSystem ).ConvertToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime)
    but pretty ugly, and two WMI requests.

  2. By: pan_2@LJ on August 13, 2009 at 6:04 am      

    with .net its better looking
    (get-date) – [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime)

  3. By: RichardSiddaway on August 13, 2009 at 2:43 pm      

    The one liner is ugly and doesn’t gain anything. Probably slower due to two WMI calls

    .NET version is better looking but looks don’t win when scripting. As long as it work ugly is good

    .NET vs WMI probably comes down to personal preference