{"id":3651,"date":"2009-08-12T15:42:34","date_gmt":"2009-08-12T15:42:34","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/08\/12\/get-systemuptime-and-working-with-the-wmi-date-format\/"},"modified":"2019-02-18T13:12:29","modified_gmt":"2019-02-18T20:12:29","slug":"get-systemuptime-and-working-with-the-wmi-date-format","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/get-systemuptime-and-working-with-the-wmi-date-format\/","title":{"rendered":"Get-SystemUptime and Working with the WMI Date Format"},"content":{"rendered":"<p>I just got a quick ping from someone at work for an uptime script.&#160; I checked in WMI with by using <a href=\"http:\/\/blogs.msdn.com\/powershell\/archive\/2007\/09\/24\/get-wmihelp-search-wmihelp.aspx\">Search-WmiHelp<\/a> to find a wmi class that had an uptime property (Win32_OperatingSystem has LastBootUpTime).&#160; 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.&#160; WMI has a couple of date time formats, and moving in and out of these formats often involves messy parsing code.&#160; 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.&#160; I guessed there might be one such class, and I used a quick <a href=\"http:\/\/blogs.msdn.com\/mediaandmicrocode\/archive\/2008\/10\/23\/microcode-powershell-scripting-tricks-exploring-net-types-with-a-get-type-function-and-reflection.aspx\">Get-Type<\/a> 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*.&#160; This led me to <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.management.managementdatetimeconverter.aspx\">System.Management.ManagementDateTimeConverter<\/a>.&#160; 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.<\/p>\n<p>With coding and with life, it\u2019s not the destination but the journey that matters most.&#160; I was able to learn some about how to work with WMI Dates in .NET and make a better uptime script.&#160; 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\u2019d written before.&#160; I hope the journey was educational. <\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #00008b\">function<\/span> <span style=\"color: #8a2be2\">Get-SystemUptime<\/span>\n<span style=\"color: #000000\">{<\/span>\n    <span style=\"color: #ff4500\">$operatingSystem<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">Get-WmiObject<\/span> <span style=\"color: #8a2be2\">Win32_OperatingSystem<\/span>\n    <span style=\"color: #008080\">[Management.ManagementDateTimeConverter]<\/span><span style=\"color: #a9a9a9\">::<\/span><span style=\"color: #000000\">ToDateTime<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$operatingSystem<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">LastBootUpTime<\/span><span style=\"color: #000000\">)<\/span>\n<span style=\"color: #000000\">}<\/span><\/pre>\n<p>Hope this Helps,<\/p>\n<p>James Brundage [MSFT]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I just got a quick ping from someone at work for an uptime script.&#160; I checked in WMI with by using Search-WmiHelp to find a wmi class that had an uptime property (Win32_OperatingSystem has LastBootUpTime).&#160; While I had found the uptime property I needed, I was left with another pretty common problem that people hit [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3651","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>I just got a quick ping from someone at work for an uptime script.&#160; I checked in WMI with by using Search-WmiHelp to find a wmi class that had an uptime property (Win32_OperatingSystem has LastBootUpTime).&#160; While I had found the uptime property I needed, I was left with another pretty common problem that people hit [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=3651"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3651\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=3651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}