<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://powershell.com/cs/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results for 'app:weblogs' matching tags 'PoweShell', 'PowerShell Scripts', and 'System.Management.DateTimeConverter.'</title><link>http://powershell.com/cs/search/SearchResults.aspx?q=app:weblogs&amp;tag=PoweShell,PowerShell+Scripts,System.Management.DateTimeConverter.&amp;orTags=0&amp;o=DateDescending</link><description>Search results for 'app:weblogs' matching tags 'PoweShell', 'PowerShell Scripts', and 'System.Management.DateTimeConverter.'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 (Build: 30929.2835)</generator><item><title>Date and Time in PowerShell</title><link>http://powershell.com/cs/blogs/powershell-scripts/archive/2009/01/14/date-and-time-in-powershell.aspx</link><pubDate>Wed, 14 Jan 2009 06:00:00 GMT</pubDate><guid isPermaLink="false">f421715f-7aba-45f0-8a8d-44de5318a3a7:878</guid><dc:creator>Anonymous</dc:creator><description>&lt;p&gt;In &lt;a href="http://pshscripts.blogspot.com/2009/01/get-uptimeps1.html"&gt;yesterday’s PowerShell script of the day&lt;/a&gt; entry,posted over on &lt;a href="http://pshscripts.blogspot.com/"&gt;my PowerShell scripts blog&lt;/a&gt;, I re-implemented an MSDN sample (originally written in VBScript) that calculates up time. The script first used a WMI class (Win32_OperatingSystem) to determine when a computer started. Then the script gets the current time, and works out and displays the difference (i.e. the currentnt uptime).&lt;/p&gt;  &lt;p&gt;The .NET framework contains a class, &lt;a href="http://pshscripts.blogspot.com/2009/01/get-uptimeps1.html"&gt;System.DateTime&lt;/a&gt;, that provides a variety of time/date related features.The PowerShell Cmdlet Get-Date returns a System.DateTime object that contains the current date and time. You can use the methods and properties of the class to get aspects of that current date and time as you need. For example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;&lt;font face="Courier New" color="#400000"&gt;PSH [D:\foo]: $d=Get-Date         &lt;br /&gt;PSH [D:\foo]: $d.Date &lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;&lt;font face="Courier New" color="#400000"&gt;14 January 2009 00:00:00 &lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;&lt;font face="Courier New" color="#400000"&gt;PSH [D:\foo]: $d.Year         &lt;br /&gt;2009          &lt;br /&gt;PSH [D:\foo]: $d.IsDaylightSavingTime()          &lt;br /&gt;False&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;You can, of course, pipe your DateTime object to Get-member to see the other properties and methods on this class (or refer to the MSDN documentation). There’s a lot of pretty rich date and time handling available.&lt;/p&gt;  &lt;p&gt;The problem I had with yesterday’s script is that WMI uses a different format for date and time. The &lt;a href="http://msdn.microsoft.com/en-us/library/aa394102(VS.85).aspx"&gt;Win32_OperatingSystem WMI object&lt;/a&gt; has a property, LastBootUpTime which returns the date and time when the OS was last booted. However, WMI returns this as a string that is formatted rather differently to System.DateTime, as you can see here:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;&lt;font face="Courier New" color="#800000"&gt;PSH [D:\foo]: $os=Get-WmiObject Win32_OperatingSystem         &lt;br /&gt;PSH [D:\foo]: $os.LastBootUpTime          &lt;br /&gt;20090112142457.454125+000&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This demonstrates that .NET, and therefore PowerShell, uses a native date/time formats that are different. For many admins (and for most native level developers) this is not a big deal. But if you need to inter-operate, you have a small issue of converting between the two formats. &lt;/p&gt;  &lt;p&gt;As it turns out, solving this issue is simple. The developers of .NET created a simple solution. First, there’s a .NET class,&amp;#160; System.Management.ManagementDateTimeConverter, which does date and time conversion. This class has a method, ToDateTime which converts a WMI date string into a .NET DateTime object. I used this&amp;#160; method in the WMIDateStringToDate function in Get-Uptime.ps1.&lt;/p&gt;  &lt;p&gt;You can either implement a function (i.e. WmiDateStringToDate) in your profile or cut/pasted it into any WMI script you write. Naturally, you could just use the .NET method natively. As follows:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;&lt;font face="Courier New" color="#800000"&gt;PSH [D:\foo]: $os=get-wmiobject win32_operatingsystem         &lt;br /&gt;PSH [D:\foo]: $time = $os.lastbootuptime          &lt;br /&gt;PSH [D:\foo]: [System.Management.ManagementDateTimeconverter]::ToDateTime($time) &lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;&lt;font face="Courier New" color="#800000"&gt;12 January 2009 14:24:58 &lt;/font&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;&lt;strong&gt;&lt;font face="Courier New" color="#800000"&gt;PSH [D:\foo]: ([System.Management.ManagementDateTimeconverter]::ToDateTime($time)).hour         &lt;br /&gt;14&lt;/font&gt;&lt;/strong&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;As with most things PowerShell, easy stuff is very easy while complex stuff is often just a method call away.&lt;/p&gt;  &lt;div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:26a673af-e1cf-4606-8ab8-9b52826c5597" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;Technorati tags: &lt;a href="http://technorati.com/tags/WMI" rel="tag"&gt;WMI&lt;/a&gt;, &lt;a href="http://technorati.com/tags/PoweShell" rel="tag"&gt;PoweShell&lt;/a&gt;, &lt;a href="http://technorati.com/tags/System.Management.DateTimeConverter." rel="tag"&gt;System.Management.DateTimeConverter.&lt;/a&gt;&lt;/div&gt;  </description></item></channel></rss>