{"id":1168,"date":"2014-06-15T00:01:00","date_gmt":"2014-06-15T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/06\/15\/weekend-scripter-use-powershell-to-explore-dates\/"},"modified":"2014-06-15T00:01:00","modified_gmt":"2014-06-15T00:01:00","slug":"weekend-scripter-use-powershell-to-explore-dates","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-explore-dates\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Explore Dates"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to explore dates in the future.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This afternoon, I am sipping a nice cup of Kwai Flower Organic Oolong tea. It is a slightly sweet, mild tea that goes well with jasmine blossoms. It makes for a nice relaxing afternoon, and the slightly woody flavor goes well with Macadamia nuts.<\/p>\n<p>Now contrast a nice relaxing afternoon with Friday the 13<sup>th<\/sup>. If I want to be prepared for the next Friday the 13<sup>th<\/sup>, how can I use Windows PowerShell to get that information for me? Well, it literally took me less than five minutes to come up with an answer.<\/p>\n<p>The key to finding dates in the future is to use the <b>DateTime<\/b> object. I am lucky because the <b>Get-Date<\/b> cmdlet returns a <b>DateTime<\/b> object. I can then directly call the <b>AddDays<\/b> method to it. I create an array of 1000 numbers and pipe it to the <b>AddDays<\/b> method to create a thousand days in the future. That should give me enough warning. If not, I can easily change it to ten thousand days.<\/p>\n<p>To get my thousand numbers to the <b>AddDays<\/b> method, I need to use the <b>Foreach-Object<\/b> cmdlet. I pipe my array of numbers to the <b>Foreach-Object<\/b> cmdlet, and I use the <b>$_ <\/b>automatic variable to represent the individual number as it comes across. I then store the newly create <b>DateTime<\/b> object in a variable I call <b>$dte<\/b>. This is shown here:<\/p>\n<p style=\"margin-left:30px\">1..1000 |<\/p>\n<p style=\"margin-left:30px\">ForEach-Object {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $dte = (Get-Date).AddDays($_)<\/p>\n<p>Now all I need to do is to use two properties from the <b>DateTime<\/b> object. The first is the <b>DayOfWeek<\/b> property. This returns the day name, and I am interested in Friday. I am only interested in Friday if the <b>Day<\/b> property is also <b>13<\/b>. This is a simple IF statement:<\/p>\n<p style=\"margin-left:30px\">If ($dte.DayOfWeek -eq &#039;Friday&#039; -AND $dte.Day -eq 13)<\/p>\n<p>If I find a match for the two conditions of a <b>DayOfWeek<\/b> of Friday and a <b>Day<\/b> equal to 13, all I need to do is to print the <b>DateTime<\/b> object for that date. This is shown here:<\/p>\n<p style=\"margin-left:30px\">{ $dte }<\/p>\n<p>And that is all there is to the script. When I run it in the Windows PowerShell ISE, I see the following in the output pane.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-6-15-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-6-15-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Surprisingly, according to my Windows PowerShell script, Friday the 13<sup>th<\/sup> only occurs two or three times a year. Sometimes less. In 2016, it only occurs once, but in 2015, it occurs three times. Dude, better start making plans now.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to explore dates in the future. Microsoft Scripting Guy, Ed Wilson, is here. This afternoon, I am sipping a nice cup of Kwai Flower Organic Oolong tea. It is a slightly sweet, mild tea that goes well with jasmine blossoms. It makes for [&hellip;]<\/p>\n","protected":false},"author":596,"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,61,45],"class_list":["post-1168","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to explore dates in the future. Microsoft Scripting Guy, Ed Wilson, is here. This afternoon, I am sipping a nice cup of Kwai Flower Organic Oolong tea. It is a slightly sweet, mild tea that goes well with jasmine blossoms. It makes for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1168","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=1168"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1168\/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=1168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}