{"id":9621,"date":"2006-09-17T00:21:00","date_gmt":"2006-09-17T00:21:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/09\/17\/the-wonders-of-date-math-using-windows-powershell\/"},"modified":"2019-02-18T13:21:19","modified_gmt":"2019-02-18T20:21:19","slug":"the-wonders-of-date-math-using-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/the-wonders-of-date-math-using-windows-powershell\/","title":{"rendered":"The Wonders of Date Math using Windows PowerShell"},"content":{"rendered":"<p>Larry Hayden posted a query about dates at:<br \/><a href=\"http:\/\/techtasks.com\/bookmarks\/powershell\/2006\/09\/interview-with-a-scripter-jeffrey-snover\/\">http:\/\/techtasks.com\/bookmarks\/powershell\/2006\/09\/interview-with-a-scripter-jeffrey-snover\/<\/a><\/p>\n<p>He has a script which gets all the Application Events that happened TODAY:<\/p>\n<p><font face=\"Courier New\" size=\"1\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<br \/>$today = [DateTime]::Today<br \/>$Events = Get-Eventlog -New 1024 Application | Where {$Today -le $_.TimeWritten}<br \/>$Events |Group EntryType,Source |Format-Table Count,Name -Auto<br \/>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/font><\/p>\n<p>What Larry really wants is the Application Events that happend in the last 24 hours.&nbsp; We&#8217;ll Larry &#8211; you&#8217;re in luck because PowerShell leverages the awesome .NET frameworks which support Date Math.&nbsp; Check this out:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <strong><font color=\"#000080\">[DateTime]::Now&nbsp; |Get-Member -MemberType Method<br \/><\/font><\/strong><br \/>&nbsp;&nbsp; TypeName: System.DateTime<br \/>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<br \/>&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<br \/>Add&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.DateTime Add(TimeSpan value)<br \/>AddDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.DateTime AddDays(Double value)<br \/>AddHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.DateTime AddHours(Double value)<br \/>AddMilliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.DateTime AddMilliseconds(Double v&#8230;<br \/>AddMinutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.DateTime AddMinutes(Double value)<br \/>AddMonths&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.DateTime AddMonths(Int32 months)<br \/>AddSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.DateTime AddSeconds(Double value)<br \/>AddTicks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.DateTime AddTicks(Int64 value)<br \/>AddYears&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.DateTime AddYears(Int32 value)<br \/>CompareTo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.Int32 CompareTo(Object value), Sy&#8230;<br \/>Equals&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.Boolean Equals(Object value), Sys&#8230;<br \/>GetDateTimeFormats&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.String[] GetDateTimeFormats(), Sy&#8230;<br \/>GetHashCode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.Int32 GetHashCode()<br \/>GetType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.Type GetType()<br \/>GetTypeCode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.TypeCode GetTypeCode()<br \/>&#8230;<br \/>IsDaylightSavingTime Method&nbsp;&nbsp;&nbsp;&nbsp; System.Boolean IsDaylightSavingTime()<br \/>Subtract&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.TimeSpan Subtract(DateTime value)&#8230;<br \/>ToBinary&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.Int64 ToBinary()<br \/>ToFileTime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.Int64 ToFileTime()<br \/>ToFileTimeUtc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.Int64 ToFileTimeUtc()<br \/>ToLocalTime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.DateTime ToLocalTime()<br \/>ToLongDateString&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.String ToLongDateString()<br \/>ToLongTimeString&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.String ToLongTimeString()<br \/>ToOADate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.Double ToOADate()<br \/>ToShortDateString&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.String ToShortDateString()<br \/>ToShortTimeString&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.String ToShortTimeString()<br \/>ToString&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.String ToString(), System.String &#8230;<br \/>ToUniversalTime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Method&nbsp;&nbsp;&nbsp;&nbsp; System.DateTime ToUniversalTime()<\/font><\/p>\n<p>So lets see this in action:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt;<strong><font color=\"#000080\"> [DateTime]::Now<br \/><\/font><\/strong><\/font><font face=\"Courier New\" size=\"1\">Saturday, September 16, 2006 9:29:29 PM<br \/><\/font><br \/><font face=\"Courier New\" size=\"1\">PS&gt; <font color=\"#000080\"><strong>[DateTime]::Now.AddDays(1)<br \/><\/strong><\/font><\/font><font face=\"Courier New\" size=\"1\">Sunday, September 17, 2006 9:29:29 PM<br \/><\/font><br \/><font face=\"Courier New\" size=\"1\">PS&gt; <font color=\"#000080\"><strong>[DateTime]::Now.AddDays(7)<br \/><\/strong><\/font><\/font><font face=\"Courier New\" size=\"1\">Saturday, September 23, 2006 9:29:30 PM<br \/><\/font><br \/><font face=\"Courier New\" size=\"1\">PS&gt; <font color=\"#000080\"><strong>[DateTime]::Now.AddDays(-1)<br \/><\/strong><\/font><\/font><font face=\"Courier New\" size=\"1\">Friday, September 15, 2006 9:29:30 PM<br \/><\/font><br \/><font face=\"Courier New\" size=\"1\">PS&gt; <strong><font color=\"#000080\">[DateTime]::Now.AddDays(-7)<br \/><\/font><\/strong><\/font><font face=\"Courier New\" size=\"1\">Saturday, September 09, 2006 9:29:31 PM<\/font><\/p>\n<p>Yes that&#8217;s right &#8211; you can provide negative numbers to AddDays() and it will do the old Way-Back Machine trick.<\/p>\n<p>So all you have to do to get the Application Events for the last 24 hours is:<\/p>\n<p><font face=\"Courier New\" size=\"1\">\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<br \/><font color=\"#ff0000\"><strong>$24HoursAgo = [DateTime]::Now.AddHours(-24)<br \/><\/strong><\/font>$Events = Get-Eventlog -New 1024 Application | Where {<font color=\"#ff0000\"><strong>$24HoursAgo<\/strong><\/font> -le $_.TimeWritten}<br \/>$Events |Group EntryType,Source |Format-Table Count,Name -Auto<br \/>\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014<\/font><\/p>\n<p>Date math VERY simple to learn once you know that it is possible and it is very useful so take a couple of minutes and try it out &#8211; it&#8217;s an investment that will pay rich dividends over the next few years.<\/p>\n<p><font face=\"Courier New\" size=\"1\"><font face=\"Times New Roman\" size=\"3\">Here is how you find out which processes have been started in the last hour:<br \/><\/font>PS&gt; <font color=\"#000080\"><strong>$1HourAgo = [DateTime]::Now.AddHours(-1)<br \/><\/strong><\/font>PS&gt;<font color=\"#000080\"> <strong>Get-Process |where {$_.StartTime -ge $1HourAgo}<\/strong><\/font><\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">Handles&nbsp; NPM(K)&nbsp;&nbsp;&nbsp; PM(K)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WS(K) VM(M)&nbsp;&nbsp; CPU(s)&nbsp;&nbsp;&nbsp;&nbsp; Id ProcessName<br \/>&#8212;&#8212;-&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211; &#8212;&#8211;&nbsp;&nbsp; &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp; &#8212; &#8212;&#8212;&#8212;&#8211;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; 52&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp;&nbsp; 1244&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9520&nbsp;&nbsp;&nbsp; 51&nbsp;&nbsp;&nbsp;&nbsp; 0.13&nbsp;&nbsp; 4240 notepad<br \/>&nbsp;&nbsp;&nbsp; 216&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8&nbsp;&nbsp;&nbsp; 32296&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 31008&nbsp;&nbsp; 173&nbsp;&nbsp;&nbsp;&nbsp; 4.39&nbsp;&nbsp; 2420 powershell<br \/>&nbsp;&nbsp;&nbsp; 117&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4&nbsp;&nbsp;&nbsp;&nbsp; 3124&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5564&nbsp;&nbsp;&nbsp; 54&nbsp;&nbsp;&nbsp;&nbsp; 0.06&nbsp;&nbsp; 5276 SearchFilterHost<br \/>&nbsp;&nbsp;&nbsp; 287&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp;&nbsp;&nbsp; 4044&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6972&nbsp;&nbsp;&nbsp; 58&nbsp;&nbsp;&nbsp;&nbsp; 0.17&nbsp;&nbsp; 2380 SearchProtocol&#8230;<\/font><\/p>\n<p>Here is how you can find out what type of files you have been working on most in the last month:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <font color=\"#000080\"><strong>dir \\ -Recurse | where {$1MonthAgo -le $_.LastWriteTime} |<br \/><\/strong><\/font>&gt;&gt;&nbsp; <font color=\"#000080\"><strong>Group Extension | sort -Descending Count |Select -First 10 |<br \/><\/strong><\/font>&gt;&gt; <font color=\"#000080\"><strong>&nbsp;Format-Table Count,Name -AutoSize<\/strong><\/font><\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">Count Name<br \/>&#8212;&#8211; &#8212;-<br \/>&nbsp; 297&nbsp;.cs<br \/>&nbsp; 249 .zip<br \/>&nbsp; 128 .ps1<br \/>&nbsp;&nbsp; 96 .txt<br \/>&nbsp;&nbsp; 90 .log<br \/>&nbsp;&nbsp; 51 .xml<br \/>&nbsp;&nbsp; 30 .dll<br \/>&nbsp;&nbsp; 21 .msg<br \/>&nbsp;&nbsp; 20 .dbf<br \/>&nbsp;&nbsp; 15 .url<\/font><\/p>\n<p>By the way &#8211; check out the readability of the scripts required to accomplish this.&nbsp;&nbsp;Imagine you wrote this script and the day after you&nbsp;installed it&nbsp;on all your production servers, you&nbsp;got hit by a beer truck.&nbsp; What are the odds that the person replacing you would be able to understand and maintain this script?&nbsp; I think they are pretty good.<\/p>\n<p>So avoid beer trucks that drive at high speeds&nbsp;on the sidewalk but write your scripts like you might not be quick enough one day.&nbsp; \ud83d\ude42&nbsp; (And apologizes in advance&nbsp;to anyone that has recently lost a loved one to a stray beer-truck.)<\/p>\n<p>Cheers!<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/Aspen Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n<p>PSMDTAG:DOTNET: DateTime<\/p>\n<p>PSMDTAG:FAQ: How do I events that occured in the last 24 hours?<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Larry Hayden posted a query about dates at:http:\/\/techtasks.com\/bookmarks\/powershell\/2006\/09\/interview-with-a-scripter-jeffrey-snover\/ He has a script which gets all the Application Events that happened TODAY: \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014$today = [DateTime]::Today$Events = Get-Eventlog -New 1024 Application | Where {$Today -le $_.TimeWritten}$Events |Group EntryType,Source |Format-Table Count,Name -Auto\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014 What Larry really wants is the Application Events that happend in the last 24 hours.&nbsp; We&#8217;ll [&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":[14,10],"class_list":["post-9621","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-dotnet","tag-faq"],"acf":[],"blog_post_summary":"<p>Larry Hayden posted a query about dates at:http:\/\/techtasks.com\/bookmarks\/powershell\/2006\/09\/interview-with-a-scripter-jeffrey-snover\/ He has a script which gets all the Application Events that happened TODAY: \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014$today = [DateTime]::Today$Events = Get-Eventlog -New 1024 Application | Where {$Today -le $_.TimeWritten}$Events |Group EntryType,Source |Format-Table Count,Name -Auto\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014 What Larry really wants is the Application Events that happend in the last 24 hours.&nbsp; We&#8217;ll [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9621","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=9621"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9621\/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=9621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=9621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=9621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}