{"id":79036,"date":"2016-06-27T00:01:14","date_gmt":"2016-06-27T07:01:14","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=79036"},"modified":"2019-02-18T09:10:34","modified_gmt":"2019-02-18T16:10:34","slug":"use-windows-powershell-to-search-for-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-windows-powershell-to-search-for-files\/","title":{"rendered":"Use Windows PowerShell to search for files"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use Get-Childitem to search the files system with PowerShell.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/> I saved a file somewhere on my computer and can\u2019t find it. Is there a way to use Windows PowerShell to find it?<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/> Honorary Scripting Guy, Sean Kearney, is here today to show you a cool trick I use all the time. I use PowerShell to search for things constantly!<\/p>\n<p>Why PowerShell? Well, to be honest, I have a bad, bad, <u>bad<\/u> habit of putting data where it shouldn\u2019t be. Sometimes I\u2019d use locations that the Indexer in Windows isn\u2019t watching. In these situations, even Cortana can\u2019t help me.<\/p>\n<p>We can use <strong>Get-Childitem<\/strong> to show a list of files and\/or directories quite easily. The following example lists all files on the root of Drive C:<\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem \u2013Path C:\\<\/code><\/p>\n<p>If we add a <strong>\u2013Recurse<\/strong> parameter, we can show <u>everything<\/u> that we have access to.<\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem \u2013Path C:\\ -Recurse<\/code><\/p>\n<p>So far, this is no different from\u00a0running the following command in the CMD prompt.<\/p>\n<p style=\"padding-left: 60px\"><code>Dir C:\\*.* \/s<\/code><\/p>\n<p>So, why PowerShell?<\/p>\n<p>For searching, PowerShell offers us all the things that we <u>don\u2019t<\/u> see under the hood. In that folder structure, there are bound to be many files that I cannot access. Those will throw an error (red by default) and makes it <u>very<\/u> hard to read.<\/p>\n<p>So, we tell PowerShell, \u201cDon\u2019t bother showing me those minor errors, just continue.\u201d<\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem \u2013Path C:\\ -Recurse -ErrorAction SilentlyContinue<\/code><\/p>\n<p>But, how do we use this as a search tool? <strong>Get-Childitem<\/strong> includes two additional parameters, <strong>-include<\/strong> and <strong>\u2013exclude<\/strong> . Their functions are pretty simple.<\/p>\n<p>The <strong>-include<\/strong> parameter says, \u201cShow me only these files in the search,\u201d and <strong>-exclude<\/strong> says, \u201cKeep that stuff out of my way.\u201d<\/p>\n<p>As the person on TV used to say, \u201cBut wait! There\u2019s more!\u201d. Sometimes the reason you can\u2019t find a file is because it was stored in the Temporary Outlook folder.<\/p>\n<p><u>That<\/u> used to drive me bananas! Because Temporary is a hidden folder, you often will miss that, and so will <strong>Get-Childitem<\/strong>. To bypass those issues, add the <strong>\u2013force<\/strong> parameter to let it examine those folders as well.<\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem \u2013Path C:\\ -Recurse \u2013force -ErrorAction SilentlyContinue<\/code><\/p>\n<p>We could now use this <u>same<\/u> command to show <u>only<\/u> the Word documents that I can access or maybe even all the files that I put the letters, HSG, in. Yes, odds are that I was watching too much Babylon 5 and stored a file in my Pictures folder.<\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem \u2013Path C:\\ -Include *HSG* -Recurse -ErrorAction SilentlyContinue<\/code><\/p>\n<p>Unfortunately, it pulls up everything, and I mean <u>everything<\/u>. with the letters, HSG, in it, including folder titles. We can tell it to show <u>only<\/u> files by using PowerShell. This was introduced in version 3 of PowerShell.<\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem \u2013Path C:\\ -Include *HSG* -File -Recurse -ErrorAction SilentlyContinue<\/code><\/p>\n<p>We can also use the the <strong>-Exclude<\/strong> parameter to say, \u201cDon\u2019t show me any TMP, MP3, or JPG files.:<\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem \u2013Path C:\\ -Include *HSG* -Exclude *.JPG,*.MP3,*.TMP -File -Recurse -ErrorAction SilentlyContinue<\/code><\/p>\n<p>Here is where things <u>really<\/u> get powerful. Let\u2019s get right down to the nitty gritty. Imagine that it was a <u>really<\/u> long weekend.<\/p>\n<p>You did the work on Friday. You even forgot the file name! Just how do you sort <u>that<\/u> out?<\/p>\n<p>In PowerShell, we can filter out files based upon date and time quite easily.<\/p>\n<p>Right now, let\u2019s get the date for June 24,\u00a02016. I\u2019m not planning on this being a long weekend but, hey, I <u>could<\/u> be sick on Monday so, it\u2019s possible \ud83d\ude09<\/p>\n<p>There are fancier ways of doing this, but I\u2019m going to use <strong>Get-Date<\/strong> because it works internationally. First, supply the year, month, and day.<\/p>\n<p style=\"padding-left: 60px\"><code>$FindDate=Get-Date -Year 2016 -Month 06 -Day 24<\/code><\/p>\n<p>With this information, I can first off target two things. First, show me all my Word documents, files only, on the entire C: drive, and keep the error messages to yourself, PowerShell.<\/p>\n<p style=\"padding-left: 60px\"><code>Get-ChildItem -Path C:\\ -Include *.doc,*.docx -File -Recurse -ErrorAction SilentlyContinue<\/code><\/p>\n<p>Now I can use <strong>Where-Object<\/strong> to show <u>only<\/u> files that were created <u>since<\/u> the day that I stored in <strong>$FindDate<\/strong>. This will include everything since 12:00 AM the morning of that day. We will compare the list against the <strong>LastWriteTime<\/strong> property, which is the \u201cLast Time the File was Written to.\u201d<\/p>\n<p style=\"padding-left: 60px\"><code>Get-ChildItem -Path C:\\ -Include *.doc,*.docx -File -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $FindDate }<\/code><\/p>\n<p>Hey, wait. I did some work on the weekend. I only want Friday! Well, we can filter on that, too, by using the <strong>AddDays()<\/strong> method to our date and give it a range of 24 hours!<\/p>\n<p style=\"padding-left: 60px\"><code>Get-ChildItem -Path C:\\ -Include *.doc,*.docx -File -Recurse -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -ge $FindDate -and $_.LastWriteTime -le $Finddate.adddays(1) }<\/code><\/p>\n<p>Of course, most people might want to search only a few spots. <strong>Get-Childitem<\/strong> can even be told to \u201cSearch only this list of folders.\u201d In the following example, I am going to search the C:\\Users folder, an HSG folder on my USB key (Drive L: ), and a folder named \u201cWhoops\\Not\\This\\One\u201d on Drive X:<\/p>\n<p style=\"padding-left: 60px\"><code>Get-Childitem -Path C:\\Users, L:\\HSG, X:\\Whoops\\Not\\This\\One -Include HSG*.doc? -Recurse<\/code><\/p>\n<p>I hope this helped you along today and empowered you a little further in your daily tasks.<\/p>\n<p>I invite you to follow the Scripting Guys 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 them 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.<\/p>\n<p>Until then always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney\n<\/strong>Honorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use Get-Childitem to search the files system with PowerShell. I saved a file somewhere on my computer and can\u2019t find it. Is there a way to use Windows PowerShell to find it? Honorary Scripting Guy, Sean Kearney, is here today to show you a cool trick I use all the time. I use PowerShell [&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":[568,685,641],"tags":[56,154,45],"class_list":["post-79036","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-scripting-techniques","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use Get-Childitem to search the files system with PowerShell. I saved a file somewhere on my computer and can\u2019t find it. Is there a way to use Windows PowerShell to find it? Honorary Scripting Guy, Sean Kearney, is here today to show you a cool trick I use all the time. I use PowerShell [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/79036","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=79036"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/79036\/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=79036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=79036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=79036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}