Welcome to MSDN Blogs Sign in | Join | Help

Get-SystemUptime and Working with the WMI Date Format

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]

Published Wednesday, August 12, 2009 8:42 PM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Get-SystemUptime and Working with the WMI Date Format

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!

Thursday, August 13, 2009 2:14 AM by Vadims Podans

# re: Get-SystemUptime and Working with the WMI Date Format

ONELINER

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

=)

Thursday, August 13, 2009 6:59 AM by pan_2@LJ

# re: Get-SystemUptime and Working with the WMI Date Format

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.

Thursday, August 13, 2009 10:19 AM by Johannes Rössel

# re: Get-SystemUptime and Working with the WMI Date Format

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.

Thursday, August 13, 2009 11:40 AM by Andy Schneider

# re: Get-SystemUptime and Working with the WMI Date Format

James - You have been talking to Jeff ;)

Thursday, August 13, 2009 4:52 PM by Jim V

# re: Get-SystemUptime and Working with the WMI Date Format

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.

Thursday, August 13, 2009 8:46 PM by PowerShellTeam

# re: Get-SystemUptime and Working with the WMI Date Format

<<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!

Thursday, August 20, 2009 10:14 AM by Erg

# re: Get-SystemUptime and Working with the WMI Date Format

Johannes:

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

Saturday, August 22, 2009 10:55 PM by Chris Charabaruk

# re: Get-SystemUptime and Working with the WMI Date Format

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.

Tuesday, September 01, 2009 9:58 AM by George

# re: Get-SystemUptime and Working with the WMI Date Format

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

[datetime](gwmi win32_operatingsystem).LastBootUpTime

Sunday, October 11, 2009 8:04 PM by Keith Hill

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker