{"id":9711,"date":"2006-09-06T11:21:00","date_gmt":"2006-09-06T11:21:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/09\/06\/datetime-utility-functions\/"},"modified":"2019-02-18T13:21:22","modified_gmt":"2019-02-18T20:21:22","slug":"datetime-utility-functions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/datetime-utility-functions\/","title":{"rendered":"DateTime Utility Functions"},"content":{"rendered":"<p>I often want to find things that happened Today.&nbsp; For instance, which files got changed today.&nbsp; Windows PowerShell makes this easy to do but it can be a bit verbose and I do it a lot so I&#8217;ve added a function to my profile:&nbsp; IsToday.<\/p>\n<p><font face=\"Courier New\" size=\"1\">function isToday ([datetime]$date) <br \/>{[datetime]::Now.Date&nbsp; -eq&nbsp; $date.Date}<\/font><\/p>\n<p>This takes advantage of 2 things:<\/p>\n<ol>\n<li>.NET provides a NOW static property which provides the current datetime\n<li>DateTimes have a DATE property which gives you just the date stripping off the specific time within that date (it always returns 12:00am).<\/li>\n<\/ol>\n<p>With this you can do things like:<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <strong><font color=\"#000080\">dir &gt; t4.txt<br \/><\/font><\/strong>PS&gt; <strong><font color=\"#000080\">dir |where {isToday $_.lastwritetime}<\/font><\/strong><\/font><br \/><font face=\"Courier New\" size=\"1\">&nbsp;&nbsp;&nbsp; Directory: Microsoft.PowerShell.Core\\FileSystem::C:\\ps<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">Mode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Length Name<br \/>&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212; &#8212;-<br \/>-a&#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9\/6\/2006&nbsp;&nbsp; 8:20 AM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 47086 t4.txt<br \/><\/font><\/p>\n<p>&nbsp;<\/p>\n<p>That is great but sometimes you want to know Recent things not just the things that happened today.&nbsp; For that I put together another function to deal with that:<\/p>\n<p><font face=\"Courier New\" size=\"1\">function isWithin([int]$days, [datetime]$Date)<br \/>{<br \/>&nbsp;&nbsp;&nbsp; [DateTime]::Now.AddDays($days).Date -le $Date.Date<br \/>}<\/font><\/p>\n<p>Note that this doesn&#8217;t deal with the case of providing dates in the future &#8211; that isn&#8217;t something I do much but if you do, this function won&#8217;t help you.<\/p>\n<p>This is how I get all the application events that occured within the last 2 days<\/p>\n<p><font face=\"Courier New\" size=\"1\">PS&gt; <strong><font color=\"#0000ff\">get-eventlog application -newest 2048 |where {isWithin -2 $_.TimeWritten}<\/font><\/strong><\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">Index Time&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type Source&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EventID Message<br \/>&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;- &#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;- &#8212;&#8212;-<br \/>15491 Sep 06 08:14&nbsp; Erro Userenv&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1054 Windows cannot obtain the domain controller name for your com&#8230;<br \/>15490 Sep 06 08:14&nbsp; Erro Userenv&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1054 Windows cannot obtain the domain controller name for your com&#8230;<br \/>15489 Sep 06 08:14&nbsp; Erro Userenv&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1054 Windows cannot obtain the domain controller name for your com&#8230;<\/font><\/p>\n<p>&#8230;.<\/p>\n<p>&nbsp;<\/p>\n<p>Enjoy!<\/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>&nbsp;<\/p>\n<p>PSMDTAG:DOTNET: Datetime<\/p>\n<p>PSMDTAG:FAQ: How can I find all the things that happened today?<\/p>\n<p>PSMDTAG:FAQ: How can I find all the things that happened within the last x days?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I often want to find things that happened Today.&nbsp; For instance, which files got changed today.&nbsp; Windows PowerShell makes this easy to do but it can be a bit verbose and I do it a lot so I&#8217;ve added a function to my profile:&nbsp; IsToday. function isToday ([datetime]$date) {[datetime]::Now.Date&nbsp; -eq&nbsp; $date.Date} This takes advantage of [&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":[67,10,68,69],"class_list":["post-9711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-datetime","tag-faq","tag-istoday","tag-iswithin"],"acf":[],"blog_post_summary":"<p>I often want to find things that happened Today.&nbsp; For instance, which files got changed today.&nbsp; Windows PowerShell makes this easy to do but it can be a bit verbose and I do it a lot so I&#8217;ve added a function to my profile:&nbsp; IsToday. function isToday ([datetime]$date) {[datetime]::Now.Date&nbsp; -eq&nbsp; $date.Date} This takes advantage of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9711","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=9711"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9711\/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=9711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=9711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=9711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}