{"id":9201,"date":"2012-06-06T00:01:00","date_gmt":"2012-06-06T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/06\/06\/use-powershell-to-find-files-modified-by-month-and-year\/"},"modified":"2012-06-06T00:01:00","modified_gmt":"2012-06-06T00:01:00","slug":"use-powershell-to-find-files-modified-by-month-and-year","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-find-files-modified-by-month-and-year\/","title":{"rendered":"Use PowerShell to Find Files Modified by Month and Year"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to find files modified by month and year.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. I will admit it. I am not the best computer user in the world. In fact, when it comes to finding things on the Internet, the Scripting Wife often is faster than I am. In my office, often the Scripting Wife sits beside me and does whatever she does. At times, I will ask her to find something that I have wasted 15 minutes seeking, and she can find it almost immediately.<\/p>\n<p>A case-in-point is Windows Search. I modified the indexing to include the full contents of my Windows PowerShell script files, and it works great. However, one thing I have not figured out used to be very easy in the Windows&nbsp;95 days by using the old-fashioned <b>Find<\/b> utility&mdash;that is to find a file by date.<\/p>\n<p>Here is the situation&hellip;<\/p>\n<p>When the Scripting Wife and I were in Montreal, Canada last October, I remember writing some additional Windows PowerShell labs for the class. In fact, I spent each day teaching the class, the evenings with friends, and the nights writing new labs for the coming day&rsquo;s class. After spending 15 minutes attempting to browse and search for these files, I finally gave up. I was unable to find my new lab files.<\/p>\n<h2>Use Windows PowerShell to find files by date<\/h2>\n<p>OK, so maybe I am not the world&rsquo;s greatest computer user, but I know Windows PowerShell really well. The neat thing is that because I do know Windows PowerShell so well, I can compensate. For example, to find my missing lab files, I use the <b>Get-ChildItem<\/b> cmdlet and search my <i>Data <\/i>directory. In addition, although I cannot remember if the files have a .<i>doc <\/i>or the newer .<i>docx <\/i>file extension, it really does not matter. Because my <i>Data <\/i>folder is deeply nested, I need to do a recursive search. The following command returns all .<i>doc <\/i>and .<i>docx <\/i>files from the <i>Data <\/i>directory on my computer.<\/p>\n<p style=\"padding-left: 30px\">Get-ChildItem -Path C:\\data -Recurse -Include *.doc,*.docx<\/p>\n<p>The next thing is that I know I modified the file during the month of October in the year of 2011. The cool thing is that the <b>LastWriteTime<\/b><i> <\/i>property is an instance of a <b>System.DateTime<\/b> object. The following script illustrates this.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Item C:\\fso\\a.txt | gm -Name lastwritetime<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; TypeName: System.IO.FileInfo<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">LastWriteTime Property&nbsp;&nbsp; System.DateTime LastWriteTime {get;set;}<\/p>\n<p>Because the <b>LastWriteTime<\/b><i> <\/i>property is an object, it means that it has a number of different properties. These properties are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; (Get-Item C:\\fso\\a.txt).lastwritetime | gm -MemberType property<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; TypeName: System.DateTime<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType Definition<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;-<\/p>\n<p style=\"padding-left: 30px\">Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.DateTime Date {get;}<\/p>\n<p style=\"padding-left: 30px\">Day&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Int32 Day {get;}<\/p>\n<p style=\"padding-left: 30px\">DayOfWeek&nbsp;&nbsp; Property&nbsp;&nbsp; System.DayOfWeek DayOfWeek {get;}<\/p>\n<p style=\"padding-left: 30px\">DayOfYear&nbsp;&nbsp; Property&nbsp;&nbsp; System.Int32 DayOfYear {get;}<\/p>\n<p style=\"padding-left: 30px\">Hour&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Int32 Hour {get;}<\/p>\n<p style=\"padding-left: 30px\">Kind&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.DateTimeKind Kind {get;}<\/p>\n<p style=\"padding-left: 30px\">Millisecond Property&nbsp;&nbsp; System.Int32 Millisecond {get;}<\/p>\n<p style=\"padding-left: 30px\">Minute&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Int32 Minute {get;}<\/p>\n<p style=\"padding-left: 30px\">Month&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Int32 Month {get;}<\/p>\n<p style=\"padding-left: 30px\">Second&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Int32 Second {get;}<\/p>\n<p style=\"padding-left: 30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Int64 Ticks {get;}<\/p>\n<p style=\"padding-left: 30px\">TimeOfDay&nbsp;&nbsp; Property&nbsp;&nbsp; System.TimeSpan TimeOfDay {get;}<\/p>\n<p style=\"padding-left: 30px\">Year&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Property&nbsp;&nbsp; System.Int32 Year {get;}<\/p>\n<p>Based on the information detailed above, I can easily look at the month and the year that file modifications took place. Therefore, I need to use the <b>Where-Object<\/b> to examine the month and year properties of each file. This is a perfect place to use the &ldquo;double-dotted&rdquo; notation. The first dot returns a <b>System.DateTime<\/b> object. The second dot returns a specific property from that <b>DateTime<\/b> object. The following code first returns the month the file was last written, and the second example returns the year that the file was modified.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; (Get-Item C:\\fso\\a.txt).lastwritetime.month<\/p>\n<p style=\"padding-left: 30px\">5<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; (Get-Item C:\\fso\\a.txt).lastwritetime.year<\/p>\n<p style=\"padding-left: 30px\">2012<\/p>\n<h2>Extract the specific properties to examine<\/h2>\n<p>By using the &ldquo;double-dotted&rdquo; technique that I discussed in the previous section, I can easily return only the files modified during October&nbsp;2011. The trick is to use the <b>$_ automatic<\/b> variable to reference the current item in the Windows PowerShell pipeline as I pipe <b>FileInfo<\/b> objects from the <b>Get-ChildItem<\/b> cmdlet to the <b>Where-Object<\/b> cmdlet. In addition, because I need to find files modified in October, but I also need to find files that were modified in 2011, I need to use a compound <b>Where-Object<\/b>, and I need to use the <b>&ndash;AND<\/b> operator. This is because I want files that were modified in the month of October AND in the year of 2011. I could look for files that were either modified in the month of October OR were modified in the year of 2011, but that would not give me what I need. So here is the <b>Where-Object<\/b> I need to use:<\/p>\n<p style=\"padding-left: 30px\">Where-Object { $_.lastwritetime.month -eq 10 -AND $_.lastwritetime.year -eq 2011 }<\/p>\n<p>The complete command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-ChildItem -Path C:\\data -Recurse -Include *.doc,*.docx |<\/p>\n<p style=\"padding-left: 30px\">Where-Object&nbsp; { $_.lastwritetime.month -eq 10 -AND $_.lastwritetime.year -eq 2011 }<\/p>\n<p>The command and the output associated with the command are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4137.hsg-6-6-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4137.hsg-6-6-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The command is actually pretty fast. On my laptop, it takes a little more than six seconds to return the files I need. I used the following command to determine that bit of information:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; measure-command {Get-ChildItem -Path C:\\data -Recurse -Include *.doc,*.docx<\/p>\n<p style=\"padding-left: 30px\">&nbsp;? { $_.lastwritetime.month -eq 10 -AND $_.lastwritetime.year -eq 2011 }}<\/p>\n<p style=\"padding-left: 30px\">Days&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Hours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Minutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0<\/p>\n<p style=\"padding-left: 30px\">Seconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 6<\/p>\n<p style=\"padding-left: 30px\">Milliseconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 147<\/p>\n<p style=\"padding-left: 30px\">Ticks&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 61477502<\/p>\n<p style=\"padding-left: 30px\">TotalDays&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;: 7.11545162037037E-05<\/p>\n<p style=\"padding-left: 30px\">TotalHours&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0.00170770838888889<\/p>\n<p style=\"padding-left: 30px\">TotalMinutes&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0.102462503333333<\/p>\n<p style=\"padding-left: 30px\">TotalSeconds&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 6.1477502<\/p>\n<p style=\"padding-left: 30px\">TotalMilliseconds : 6147.7502<\/p>\n<p>So how many files is that? Well, it is more than 46,000 files. The following command tells me that:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-ChildItem -Path C:\\data -Recurse | Measure-Object<\/p>\n<p style=\"padding-left: 30px\">Count&nbsp;&nbsp;&nbsp; : 46425<\/p>\n<p style=\"padding-left: 30px\">Average&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Sum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Maximum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Minimum&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Property :<\/p>\n<p>So what is my point?<\/p>\n<p>If you get really good with Windows PowerShell, you improve more than just your system admin skills. Personally, I use Windows PowerShell every day; and these days, I do not do that much actual system administration. I love the Windows Search tool, and I am certain it could help me find files that I modified in October of last year. But it actually took me less than a minute to whip out the command, and it worked the very first time I ran it. Not only that, but the command is pretty fast as well. So given that I can find stuff in less than a minute, it does not really pay for me to spend hours trying to find out how to search by &ldquo;date modified.&rdquo; Windows PowerShell makes it easy. Join me tomorrow for more Windows PowerShell cool stuff.<\/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, talks about using Windows PowerShell to find files modified by month and year. Microsoft Scripting Guy, Ed Wilson, is here. I will admit it. I am not the best computer user in the world. In fact, when it comes to finding things on the Internet, the Scripting Wife often [&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":[38,3,4,45],"class_list":["post-9201","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to find files modified by month and year. Microsoft Scripting Guy, Ed Wilson, is here. I will admit it. I am not the best computer user in the world. In fact, when it comes to finding things on the Internet, the Scripting Wife often [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9201","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=9201"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9201\/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=9201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=9201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=9201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}