Get-SystemUptime and Working with the WMI Date Format

Get-SystemUptime and Working with the WMI Date Format

Rate This
  • Comments 10

I just got a quick ping from someone at work for an uptime script.  I checked in WMI with by using Search-WmiHelp to find a wmi class that had an uptime property (Win32_OperatingSystem has LastBootUpTime).  While I had found the uptime property I needed, I was left with another pretty common problem that people hit (in VBScript and in PowerShell) about dealing with WMI times.  WMI has a couple of date time formats, and moving in and out of these formats often involves messy parsing code.  Luckily, the WMI team was nice enough to make sure that the .NET classes you use to work with WMI can convert dates, times, and timespans.  I guessed there might be one such class, and I used a quick Get-Type function to get all loaded types, and then piped the results into Where-Object to find a type where the fullname was like *Management*DateTime*.  This led me to System.Management.ManagementDateTimeConverter.  Not only does this class have the missing chunk to turn a WMI Time into a DateTime, but it also has the methods to turn it back and it has similar methods for timespans.

With coding and with life, it’s not the destination but the journey that matters most.  I was able to learn some about how to work with WMI Dates in .NET and make a better uptime script.  I was able to do most of this building upon the richness of PowerShell and I was able to work through the sea of types with scripts I’d written before.  I hope the journey was educational.

function Get-SystemUptime            
{            
    $operatingSystem = Get-WmiObject Win32_OperatingSystem                
    [Management.ManagementDateTimeConverter]::ToDateTime($operatingSystem.LastBootUpTime)            
}

Hope this Helps,

James Brundage [MSFT]

Leave a Comment
  • Please add 4 and 5 and type the answer here:
  • Post
  • I have already wroted this script:

    http://www.sysadmins.lv/PermaLink,guid,86f52f41-e341-459c-a781-dbd5e0861113.aspx

    this show actual uptime value (not only when system was booted).

    thanks, anyway!

  • ONELINER

    [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime)

    =)

  • Nice to know. A minot nitpick, though: The function should be either named Get-SystemBootUpTime (or similar) or actually return a TimeSpan referring to the current uptime.

  • You can also use the WMI Accelerator

    ([WMI]'').ConvertToDateTime((gwmi win32_operatingsystem).LastBootUpTime)

    Its not static so you have to create an empty WMI object and call the ConvertToDateTime method.

  • James - You have been talking to Jeff ;)

  • Which Jeff?  I talk to Jeffrey Snover on a regular basis, as we're all on PowerShell, but this did not come from Jeffrey.  It was a Mike that asked for uptime.

  • <<With coding and with life, it’s not the destination but the journey that matters most. >>

    With life, definitely.  With coding, well, I've learned over the years that if you're trying to make a living selling commercial software, is the destination that counts more than anything.  Customers don't care about the journey.  Thanks for the post!

  • Johannes:

    [DateTime]::Now - [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime))

  • But this actually fails to take into consideration the possibility that the system is temporarily put into sleep or hibernation. In Windows Vista and 7, the property "Up Time" in Task Manager -> Performance -> System gives the correct uptime, which subtracts the sleep time from the difference Johannes suggested.

  • Or if you're using the PowerShell Community Extensions, just use type cast using the datetime accelerator e.g.:

    [datetime](gwmi win32_operatingsystem).LastBootUpTime

Page 1 of 1 (10 items)