{"id":17531,"date":"2010-08-05T00:01:00","date_gmt":"2010-08-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/08\/05\/manipulating-dates-returned-by-windows-management-instrumentation-wmi\/"},"modified":"2010-08-05T00:01:00","modified_gmt":"2010-08-05T00:01:00","slug":"manipulating-dates-returned-by-windows-management-instrumentation-wmi","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/manipulating-dates-returned-by-windows-management-instrumentation-wmi\/","title":{"rendered":"Manipulating Dates Returned by Windows Management Instrumentation (WMI)"},"content":{"rendered":"<p>&nbsp;\n<img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\">\nHey, Scripting Guy! I need to understand how to work with dates that I receive from Windows Management Instrumentation (<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394582(VS.85).aspx\">WMI<\/a>). I remember in VBScript I had to use a function I got from the Scripting Guys that did string manipulation to convert the WMI date to a &ldquo;normal&rdquo; date. I guess that is okay, but I had no idea how that function worked or how to correct things when it did not work properly. I have been reading your <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/\">Hey, Scripting Guy! Blog<\/a> posts this week about dates, and I have yet to see you address this issue. Is it because it is too hard, and you are going to do the old two-step shuffle?\n&#8212; EA\n&nbsp;\n<img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\">\nHello EA,\nMicrosoft Scripting Guy Ed Wilson here. As a famous twentieth century philosopher once said, &ldquo;<a href=\"http:\/\/en.wikipedia.org\/wiki\/I_Can%27t_Dance\">I can&#8217;t dance<\/a>&rdquo;; therefore, I do not know how to do the old <a href=\"http:\/\/en.wikipedia.org\/wiki\/Country-western_two-step\">two-step<\/a> shuffle. Or the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ickey_Shuffle\">Ickey Shuffle<\/a>. I&rsquo;m not too bad with the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Shuffle\">Corgi Shuffle<\/a> though.\nI can do a pretty effective string shuffle. It is string shuffling that is required to make sense of the date string that is returned by WMI. This is because <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa389802(VS.85).aspx\">WMI dates are returned as strings<\/a> and not as <b>datetime<\/b> objects. These strings are compliant with the Distributed Management Task Force (DMTF) Common Information Model (CIM) standards. In VBScript days, most scripters used a version of the function found in the <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/03a18f58-0e13-47d1-8844-b6821272a914\">Convert WMI Date-Time Values script<\/a> in the Scripting Guys Script Repository. EA, you are probably talking about this VBScript function in your email. It was a brilliant piece of code and is shown here:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">WMIDateStringToDate(dtmInstallDate)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">WMIDateStringToDate<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">CDate(Mid(dtmInstallDate,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">5<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&amp;<\/span><span style=\"color: #808080\"> &#8220;\/&#8221; <\/span><span style=\"color: #000000\">&amp;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">_<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Mid(dtmInstallDate,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">7<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&amp;<\/span><span style=\"color: #808080\"> &#8220;\/&#8221; <\/span><span style=\"color: #000000\">&amp;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Left(dtmInstallDate,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">_<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">&amp;<\/span><span style=\"color: #808080\"> &#8221; &#8221; <\/span><span style=\"color: #000000\">&amp;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Mid<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(dtmInstallDate,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">9<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&amp;<\/span><span style=\"color: #808080\"> &#8220;:&#8221; <\/span><span style=\"color: #000000\">&amp;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">_<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Mid(dtmInstallDate,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">11<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&amp;<\/span><span style=\"color: #808080\"> &#8220;:&#8221; <\/span><span style=\"color: #000000\">&amp;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Mid(dtmInstallDate,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">_<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #800000\">13<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">))<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">End<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Function<\/span>\nInterestingly enough, in <a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=6ccb7e0d-8f1d-4b97-a397-47bcc8ba3806\">Windows XP<\/a> a new COM object was introduced to WMI&mdash;the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa393687(VS.85).aspx\">SwbemDateTime object<\/a>. This object obsoleted the <b>WMIDateStringToDate<\/b> function. This object is discussed in a Hey, Scripting Guy! <a href=\"http:\/\/technet.microsoft.com\/en-us\/magazine\/\">TechNet Magazine<\/a> article called <a href=\"http:\/\/technet.microsoft.com\/en-us\/magazine\/2006.07.scriptingguy.aspx\">It&rsquo;s About Time (Oh, and About Dates, Too)<\/a>. To use the <b>SwbemDateTime<\/b> object, I wrote the <b>FunDate<\/b> function, which is much easier to understand and is more reliable&mdash;and perhaps fun! <\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">funDate(wmiDate)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Dim<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">objSWbemDateTime<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">Set<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">objSWbemDateTime<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">CreateObject(<\/span><span style=\"color: #808080\">&#8220;WbemScripting.SWbemDateTime&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">objSWbemDateTime.value<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">wmiDate<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">funDate<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">objSWbemDateTime.GetVarDate<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">End<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span>\nAn example of a WMI UTC date is seen here from the <b>InstallDate<\/b> property of the <b>Win32_OperatingSystem<\/b> WMI class. <\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-WmiObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">win32_operatingsystem<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #000000\">.InstallDate<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #800000\">20091024192037<\/span><span style=\"color: #000000\">.000000-240<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span>\nEither of these approaches can be leveraged in <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/powershell.aspx\">Windows PowerShell<\/a>. To use the <b>SwbemDateTime<\/b> object, use the <b>New-Object<\/b> cmdlet to create an instance of the object. Use the <b>Get-WmiObject<\/b> cmdlet to perform the WMI query of the <b>Win32_OperatingSystem<\/b> class, supply the <b>InstallDate<\/b> value to the value property of the <b>SwbemDateTime<\/b> object, and call the <b>GetVarDate<\/b> method from the same object. This technique is illustrated here:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$swbem<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ComObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">wbemscripting.swbemdatetime<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-WmiObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">win32_operatingsystem<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$swbem<\/span><span style=\"color: #000000\">.Value<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #000000\">.InstallDate<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$swbem<\/span><span style=\"color: #000000\">.GetVarDate()<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Saturday,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">October<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">24<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2009<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">7<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">20<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">37<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">PM<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span>\nBut the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.management.managementdatetimeconverter_members.aspx\">System.Management.ManagementDateTimeConverter<\/a> .NET Framework class contains a number of static methods that are useful for working with WMI Dates. The static methods are shown here: <\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">PS C:&gt; [management.managementDateTimeConverter] | gm -s<\/p>\n<p><span>&nbsp;&nbsp; <\/span>TypeName: System.Management.ManagementDateTimeConverter<\/p>\n<p>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>MemberType Definition<br \/>&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br \/>Equals<span>&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>static bool Equals(System.Object objA, System.Objec&#8230;<br \/>ReferenceEquals<span>&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>static bool ReferenceEquals(System.Object objA, Sys&#8230;<br \/>ToDateTime<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>static System.DateTime ToDateTime(string dmtfDate)<br \/>ToDmtfDateTime<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>static string ToDmtfDateTime(System.DateTime date)<br \/>ToDmtfTimeInterval Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>static string ToDmtfTimeInterval(System.TimeSpan ti&#8230;<br \/>ToTimeSpan<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Method<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>static System.TimeSpan ToTimeSpan(string dmtfTimespan)<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/span><\/span>\nTo parse the <b>InstallDate<\/b> property from the <b>Win32_OperatingSystem<\/b> WMI class, you would use the <b>ToDateTime<\/b> static method as illustrated here:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-WmiObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">win32_operatingsystem<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[management.managementDateTimeConverter]::ToDateTime(<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #000000\">.InstallDate)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Saturday,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">October<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">24<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2009<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">7<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">20<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">37<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">PM<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span>\nThis can be accomplished in one line, by retrieving both the WMI class and selecting the property at the same time.\n<img decoding=\"async\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/august\/hey0805\/hsg-08-05-10-01.jpg\" alt=\"Image of retrieving WMI class and selecting property at same time\" style=\"max-width: 550px;border: 0px\">\nThere is only one problem with using the <b>management.managementDateTimeConverter<\/b> .NET Framework class&mdash;it is waaaaaaaaaaay toooooooooooo loooooong for me to type reliably on a regular basis. Therefore, this makes a great candidate to add to one&rsquo;s <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/11\/23\/hey-scripting-guy-how-can-i-use-profiles-with-windows-powershell.aspx\">profile<\/a>. Before I add anything to a profile, I like to test the command first to ensure it works. Here is the command and the &ldquo;test&rdquo;:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Variable<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">dteConv<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Value<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #808080\">&#8220;management.managementDateTimeConverter&#8221; <br \/><\/span><span style=\"color: #000000\">-as<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #0000ff\">type<\/span><span style=\"color: #000000\">])<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Description<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">MrEd_Variable<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$dteConv<\/span><span style=\"color: #000000\">::ToDateTime((<\/span><span style=\"color: #0000ff\">gwmi<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">win32_operatingSystem).InstallDate)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Saturday,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">October<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">24<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2009<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">7<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">20<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">37<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">PM<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span>\nI can edit a Windows PowerShell profile in Notepad, as shown in the following image. (Windows PowerShell profiles were discussed in <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/getting+started\/profiles\/\">Hey, Scripting Guy! Blog posts<\/a> during Profile Week.)\n<img decoding=\"async\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/august\/hey0805\/hsg-08-05-10-02.jpg\" alt=\"?Image of editing Windows PowerShell profile in Notepad\" style=\"max-width: 550px;border: 0px\">\nThere is another way to deal with WMI dates and times, and that is to use a <b>ScriptMethod<\/b> that is added by the Windows PowerShell team to WMI objects. Two <b>ScriptMethod<\/b>s are added, as shown via the <b>Get-Member<\/b> cmdlet:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\"><span style=\"font-size: 10pt\"><span style=\"font-family: Lucida Sans Typewriter\">PS C:&gt; Get-WmiObject win32_OperatingSystem | Get-Member -MemberType ScriptMethod<\/p>\n<p><span>&nbsp;&nbsp; <\/span>TypeName: System.Management.ManagementObject#rootcimv2Win32_OperatingSystem<\/p>\n<p>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>MemberType<span>&nbsp;&nbsp; <\/span>Definition<br \/>&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;-<span>&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;-<br \/>ConvertFromDateTime ScriptMethod System.Object ConvertFromDateTime();<br \/>ConvertToDateTime<span>&nbsp;&nbsp; <\/span>ScriptMethod System.Object ConvertToDateTime();<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/span><\/span>\nBecause the <b>ScriptMethod<\/b>s are added directly to instances of the WMI class, they can be accessed directly:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-WmiObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">win32_OperatingSystem<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #000000\">.ConvertToDateTime(<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #000000\">.InstallDate)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Saturday,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">October<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">24<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2009<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">7<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">20<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">37<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">PM<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span>\nUsing the <b>ScriptMethod<\/b> always involves two steps: creating an instance of the WMI object, and passing an instance with the property to the <b>ScriptMethod<\/b>. The only way to perform this on a single line is to use the semicolon to create two logical commands on a single physical line. This is shown here:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-WmiObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">win32_OperatingSystem<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #000000\">.ConvertToDateTime(<\/span><span style=\"color: #2b91af\">$os<\/span><span style=\"color: #000000\">.Install<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Date)<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #000000\">Saturday,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">October<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">24<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2009<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">7<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">20<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #800000\">37<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">PM<\/span><span style=\"color: #808080\"> <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:&gt;<\/span>\nThe advantage of always having access to the <b>ConvertToDateTime<\/b> <b>ScriptMethod<\/b> on an instance of a WMI class can be seen here. The <b>gwmi<\/b> alias for the <b>Get-WmiObject<\/b> cmdlet and the <b>ft<\/b> alias for the <b>Format-Table<\/b> cmdlet are used. A custom label is created for the table called &ldquo;Installed&rdquo;. The value of the installed date is created by using the <b>ConvertToDateTime<\/b> <b>ScriptMethod<\/b>. The results can be compared to creating a table with the version and the <b>installdate<\/b> without parsing the date:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: 'Segoe','sans-serif';color: black;font-size: 10pt\">PS C:&gt; gwmi win32_operatingsystem | ft version, @{LABEL=&#8221;Installed&#8221;; EXPRESSION = {$<br \/>_.convertToDateTime($_.InstallDate)}}<\/p>\n<p>version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Installed<br \/>&#8212;&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;<br \/>6.1.7600<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>10\/24\/2009 7:20:37 PM<\/p>\n<p>PS C:&gt; gwmi win32_operatingsystem | ft version, installDate<\/p>\n<p>version<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>installDate<br \/>&#8212;&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;&#8211;<br \/>6.1.7600<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>20091024192037.000000-240<\/p>\n<p>PS C:&gt;<\/span>\n<span style=\"font-family: 'Segoe','sans-serif';color: black;font-size: 10pt\"><br><\/span>EA, that is all there is to using dates when working with Windows Management Instrumentation (WMI). That also brings Date Week to a close. Join us tomorrow for <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/quick_2d00_hits+friday\/\">Quick-Hits Friday<\/a>.\nWe invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n&nbsp;\n<b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b>\n<strong><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! I need to understand how to work with dates that I receive from Windows Management Instrumentation (WMI). I remember in VBScript I had to use a function I got from the Scripting Guys that did string manipulation to convert the WMI date to a &ldquo;normal&rdquo; date. I guess that is okay, [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[13,3,4,45],"class_list":["post-17531","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! I need to understand how to work with dates that I receive from Windows Management Instrumentation (WMI). I remember in VBScript I had to use a function I got from the Scripting Guys that did string manipulation to convert the WMI date to a &ldquo;normal&rdquo; date. I guess that is okay, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17531","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17531"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17531\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}