{"id":13111,"date":"2011-08-04T00:01:00","date_gmt":"2011-08-04T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/08\/04\/use-an-easy-powershell-command-to-search-files-for-information\/"},"modified":"2011-08-04T00:01:00","modified_gmt":"2011-08-04T00:01:00","slug":"use-an-easy-powershell-command-to-search-files-for-information","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-an-easy-powershell-command-to-search-files-for-information\/","title":{"rendered":"Use an Easy PowerShell Command to Search Files for Information"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use a Windows PowerShell command to search easily for information in a collection of files.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hey, Scripting Guy! I need to be able to parse multiple files for text that are in a single folder. I hate to have to write a script for such a common task, but I am afraid I will have to do so. Can you help me?<\/p>\n<p>&mdash;SH<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hello SH,<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. The Scripting Wife and I are trying to get things sorted out this week before we leave for Corpus Christi, Texas, where I will be teaching a Windows PowerShell class. In addition, we will be appearing at the <a href=\"http:\/\/powershellgroup.org\/corpus.tx\">inaugural meeting of the Corpus Christi PowerShell User Group meeting<\/a>. If you will be in South Texas on August 9, 2011, you should come check it out. It should be great fun. Oh, by the way, I am doing a <a href=\"http:\/\/lincoln.sqlpass.org\/\">meeting today with Lincoln SQL Server User Group (ssug)<\/a>. The meeting will be available via Live Meeting. It feels like this week started late and will end early. Luckily, SH, the answer to your question is no, you do not have to write a script to parse a folder full of files for a particular string. In fact, it was a topic that was tested in the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/2011+scripting+games\/event+6\/beginner\/\">Beginner Event 6 in the 2011 Scripting Games<\/a>.<\/p>\n<p>The solution is to use the <b>Select-String<\/b> cmdlet. One thing to keep in mind is that the <b>Select-String<\/b> cmdlet reads text files; it cannot read the more complicated file types such as .doc and .docx files that are generated by Microsoft Word. When I attempted to search a folder containing the Word documents and pictures that make up a typical Hey, Scripting Guy! Blog post, Windows PowerShell displayed a bunch of gibberish in the console, and then locked up. This is shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2605.HSG-8-4-11-01.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of gibberish output\" alt=\"Image of gibberish output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2605.HSG-8-4-11-01.png\" \/><\/a><\/p>\n<p>The easy way to avoid producing gibberish is to specify the file types you want to search. For example, if I want to search all text files in the c:\\fso directory for a pattern of <i>ed <\/i>(such as my first name), I include a wildcard character in my path specification, and choose any file that has the file extension of .<i>txt. <\/i>The nice thing about the <b>Select-String<\/b> cmdlet is that it expects the <i>path<\/i> as well as the <i>pattern <\/i>parameter to be strings, so I do not need to use quotation marks for either the pattern or the path. I can use the following command to search the c:\\fso folder for files that have the .<i>txt <\/i>file extension, and contain a pattern match for <i>ed<\/i>:<i><\/i><\/p>\n<p style=\"padding-left: 30px\">Select-String -Path c:\\fso\\*.txt -pattern ed<\/p>\n<p>The command and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0572.HSG-8-4-11-02.png\"><\/a><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8510.HSG-8-4-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8510.HSG-8-4-11-02.png\" \/><\/a><\/p>\n<p>If I use the <b>Get-Command<\/b> cmdlet (<b>gcm<\/b> is an alias for this cmdlet) to examine the syntax for the <b>Select-String<\/b> cmdlet, I see that both the <i>path <\/i>and the <i>pattern <\/i>parameters will accept an array of strings. This means that I can use the wildcard character trick with the file extensions to look for multiple files at the same time. To examine only the syntax of the <b>Select-String<\/b> cmdlet, I used the <b>Get-Command<\/b> cmdlet and piped the output to the <b>Select-Object<\/b> cmdlet (<b>select<\/b> is an alias). I then chose to expand the <b>definition<\/b> property. The resulting command is shown here:<\/p>\n<p style=\"padding-left: 30px\">gcm select-string | select -expand definition<\/p>\n<p>The command and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3716.HSG-8-4-11-03.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3716.HSG-8-4-11-03.png\" \/><\/a><\/p>\n<p>Because I can supply an array of strings to the <i>path <\/i>parameter, I can search for both .<i>log <\/i>files and .<i>txt <\/i>files at the same time. In my revised <b>Select-String<\/b> command, I search the c:\\fso folder for both .<i>txt <\/i>and .<i>log <\/i>files. I look inside both types of files for a pattern match of <i>ed.<\/i> The revised command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Select-String -Path c:\\fso\\*.txt, c:\\fso\\*.log -pattern ed<b><\/b><\/p>\n<p>The command and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4338.HSG-8-4-11-04.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4338.HSG-8-4-11-04.png\" \/><\/a><\/p>\n<p>Because the <i>pattern <\/i>parameter also accepts an array of strings, I can also search the .<i>txt <\/i>and the .<i>log <\/i>files for both <i>ed<\/i> and <i>teresa <\/i>strings. The command to search the c:\\fso folder for both .<i>txt<\/i> and for .<i>log<\/i> files, and to look for pattern matches with both <i>ed<\/i> and <i>teresa<\/i> is shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3034.HSG-8-4-11-05.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of searching folder and pattern matching\" alt=\"Image of searching folder and pattern matching\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3034.HSG-8-4-11-05.png\" \/><\/a><\/p>\n<p>In addition to directly using the <i>path <\/i>parameter in the <b>Select-String<\/b> cmdlet, it may be easier to use the <b>Get-Childitem<\/b> cmdlet for more granular control over the files to be parsed. In the following command, I use the <b>dir<\/b><i> <\/i>command (an alias for the <b>Get-ChildItem<\/b> cmdlet) and provide the path of c:\\fso (the path does not appear in the command because it is the default parameter). I include only .<i>txt <\/i>and .<i>log <\/i>files (I use the <b>&ndash;I <\/b>and rely on parameter completion to specify the <i>include<\/i> parameter. I do the same thing with the <i>recurse<\/i> switch (in that I just use the letter <b>r<\/b>). I pipe the results to the <b>Select-String<\/b> cmdlet and look for the pattern <i>fail <\/i>(<i>pattern<\/i> is the default parameter and therefore is omitted in the command). The long version of the command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Get-ChildItem -Path c:\\fso -Include *.txt, *.log -Recurse | Select-String -Pattern fail<\/p>\n<p>Here is an example of the shorter form of the command.<\/p>\n<p style=\"padding-left: 30px\">dir c:\\fso -I *.txt, *.log -R | Select-String fail<\/p>\n<p>The command and associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8228.HSG-8-4-11-06.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8228.HSG-8-4-11-06.png\" \/><\/a><\/p>\n<p>Interestingly enough, the above output displays information from an install.log file, and it shows a bunch of failures. I decide that I would like to see the successes as well as the failures. I modify the command by adding the word <i>success<\/i> to the pattern. The revised command is shown here:<\/p>\n<p style=\"padding-left: 30px\">dir c:\\fso -I *.txt, *.log -R | Select-String fail, success<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1258.HSG-8-4-11-07.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of all successes being client workstations\" alt=\"Image of all successes being client workstations\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1258.HSG-8-4-11-07.png\" \/><\/a>&nbsp;<\/p>\n<p>As I look over the output from the previous command, I see a pattern appearing: on all the servers, the installation failed. On the client computers, the installation was a success. But I am missing my Windows XP computers in the output. I decide to add the word <i>pending<\/i> to my array of search terms. Here is the revised command:<\/p>\n<p style=\"padding-left: 30px\">dir c:\\fso -I *.txt, *.log -R | Select-String fail, success, pending<\/p>\n<p>The command and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2476.HSG-8-4-11-08.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2476.HSG-8-4-11-08.png\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Well, SH, thank you for your question. I hope I have encouraged you to spend a bit more time exploring the <b>Select-String<\/b> cmdlet.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and to join the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">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><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use a Windows PowerShell command to search easily for information in a collection of files. &nbsp; Hey, Scripting Guy! I need to be able to parse multiple files for text that are in a single folder. I hate to have to write a script for such a common task, but I [&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":[3,4,281,21,14,45],"class_list":["post-13111","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-select-string","tag-string-manipulation","tag-text-files","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use a Windows PowerShell command to search easily for information in a collection of files. &nbsp; Hey, Scripting Guy! I need to be able to parse multiple files for text that are in a single folder. I hate to have to write a script for such a common task, but I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13111","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=13111"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13111\/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=13111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}