{"id":6491,"date":"2008-03-23T15:22:31","date_gmt":"2008-03-23T15:22:31","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2008\/03\/23\/select-string-and-grep\/"},"modified":"2019-02-18T13:16:06","modified_gmt":"2019-02-18T20:16:06","slug":"select-string-and-grep","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/select-string-and-grep\/","title":{"rendered":"Select-String and Grep"},"content":{"rendered":"<p>Dustin Marx has a blog entry where he compares <a href=\"http:\/\/marxsoftware.blogspot.com\/2008\/03\/comparing-unixlinux-powershell-and.html\">Unix\/Linux, PowerShell and DOS commands<\/a>.&#160; In it he says, &quot;<em>If there is one Unix command I would love to have in PowerShell, it is the grep command with its regular expression support.<\/em>&quot;&#160; Well Dustin, your wish is our command.&#160; Select-String command to be precise:<\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt; <strong>Get-Help Select-String<\/strong><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">NAME     <br \/>&#160;&#160;&#160; Select-String <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">SYNOPSIS     <br \/>&#160;&#160;&#160; Identifies patterns in strings. <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">SYNTAX     <br \/>&#160;&#160;&#160; Select-String [-pattern] &lt;string[]&gt; -inputObject &lt;psobject&gt; [-include &lt;stri      <br \/>&#160;&#160;&#160; ng[]&gt;] [-exclude &lt;string[]&gt;] [-simpleMatch] [-caseSensitive] [-quiet] [-lis      <br \/>&#160;&#160;&#160; t] [&lt;CommonParameters&gt;] <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&#160;&#160;&#160; Select-String [-pattern] &lt;string[]&gt; [-path] &lt;string[]&gt; [-include &lt;string[]&gt;     <br \/>&#160;&#160;&#160; ] [-exclude &lt;string[]&gt;] [-simpleMatch] [-caseSensitive] [-quiet] [-list] [&lt;      <br \/>&#160;&#160;&#160; CommonParameters&gt;] <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">DETAILED DESCRIPTION     <br \/>&#160;&#160;&#160; Identifies patterns in strings. By default, Select-String interprets the va      <br \/>&#160;&#160;&#160; lue of the Pattern parameter as a regular expression and matches input agai      <br \/>&#160;&#160;&#160; nst it. To learn more about regular expressions in Windows PowerShell, type      <br \/>&#160;&#160;&#160;&#160; get-help about_regular_expression. You can suppress the regular expression      <br \/>&#160;&#160;&#160;&#160; match by using the SimpleMatch parameter. A simple match attempts to find      <br \/>&#160;&#160;&#160; the string specified in the Pattern parameter as a substring of the input. <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&#160;&#160;&#160; The cmdlet makes it easy to search string content from files. It includes a     <br \/>&#160;&#160;&#160;&#160; Path parameter that supports wildcards and when that parameter is used, th      <br \/>&#160;&#160;&#160; e contents of the referenced files are retrieved and matched against the va      <br \/>&#160;&#160;&#160; lue of the Pattern parameter. <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&#160;&#160;&#160; Output from the cmdlet is, by default, a MatchInfo object which includes de     <br \/>&#160;&#160;&#160; tailed information about the matches. The information is most useful when t      <br \/>&#160;&#160;&#160; he input to the cmdlet is retrieved from files. The object includes propert      <br \/>&#160;&#160;&#160; ies like Filename and Line, which have the value &#8216;InputStream&#8217; when the inp      <br \/>&#160;&#160;&#160; ut was not from a file. You can use the Quiet parameter to suppress the out      <br \/>&#160;&#160;&#160; put of MatchInfo objects. In that case, the resulting output becomes a bool      <br \/>&#160;&#160;&#160; ean value that is true if a match occurred and false otherwise. <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&#160;&#160;&#160; When matching file content, you can use the List parameter to stop after th     <br \/>&#160;&#160;&#160; e first match in each input file. You should use this parameter if you only      <br \/>&#160;&#160;&#160;&#160; require a single match, because it will result in faster matching commands.<\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\"><\/font><\/p>\n<p>There are a ton of great scenarios but here are some of the more common usages:<\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt; <strong>dir . -recurse |%{ &quot;`n*** $($_.name)&quot;; cat $_} <\/strong><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">*** animals.txt     <br \/>dog      <br \/>cat      <br \/>horse      <br \/>cow <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">*** fruit.txt     <br \/>orange      <br \/>apple      <br \/>cherry <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">*** trees.txt     <br \/>Elm      <br \/>Maple      <br \/>Oak      <br \/>Dogwood      <br \/>Apple <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt; <strong>Set-Alias ss Select-String <\/strong><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt; <strong>ss Dog *<\/strong> <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">animals.txt:1:dog     <br \/>trees.txt:4:Dogwood <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt; <strong>ss Dog * -CaseSensitive <\/strong><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">trees.txt:4:Dogwood <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt; <strong>ss ^[cd]o *<\/strong> <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">animals.txt:1:dog     <br \/>animals.txt:4:cow      <br \/>trees.txt:4:Dogwood <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt; <strong>ss ^[cd]o -path * -Exclude *an*.txt<\/strong> <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">trees.txt:4:Dogwood<\/font><\/p>\n<p>&#160;<\/p>\n<p>We&#8217;ve expanded Select-String in the next version with a number of additional functions.&#160; One of my favorites is -Context which allows you to specify the number of lines&#160; you want displayed before and after a match.&#160; Check it out:<\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt;<strong> ss oak * <\/strong><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">trees.txt:3:Oak <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt; <strong>ss oak * -Context 1,0<\/strong> <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&#160; trees.txt:2:Maple     <br \/>&gt; trees.txt:3:Oak <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt; <strong>ss oak * -Context 0,1<\/strong> <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&gt; trees.txt:3:Oak     <br \/>&#160; trees.txt:4:Dogwood <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt; <strong>ss oak * -Context 2,1<\/strong> <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">&#160; trees.txt:1:Elm     <br \/>&#160; trees.txt:2:Maple      <br \/>&gt; trees.txt:3:Oak      <br \/>&#160; trees.txt:4:Dogwood <\/font><\/p>\n<p>And last but not least, this is PowerShell so of course we are not going to just emit text, we emit objects which <\/p>\n<p><font face=\"Courier New\" size=\"2\">PS&gt; <strong>ss dog * |fl * <\/strong><\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">IgnoreCase : True     <br \/>LineNumber : 1      <br \/>Line&#160;&#160;&#160;&#160;&#160;&#160; : dog      <br \/>Filename&#160;&#160; : animals.txt      <br \/>Path&#160;&#160;&#160;&#160;&#160;&#160; : C:\\temp\\ss\\animals.txt      <br \/>Pattern&#160;&#160;&#160; : dog <\/font><\/p>\n<p><font face=\"Courier New\" size=\"2\">IgnoreCase : True     <br \/>LineNumber : 4      <br \/>Line&#160;&#160;&#160;&#160;&#160;&#160; : Dogwood      <br \/>Filename&#160;&#160; : trees.txt      <br \/>Path&#160;&#160;&#160;&#160;&#160;&#160; : C:\\temp\\ss\\trees.txt      <br \/>Pattern&#160;&#160;&#160; : dog<\/font><\/p>\n<p>We should have produced an alias from grep to Select-String.<\/p>\n<p>Enjoy!<\/p>\n<p>Jeffrey Snover [MSFT]   <br \/>Windows Management Partner Architect    <br \/>Visit the Windows PowerShell Team blog at:&#160;&#160;&#160; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a>    <br \/>Visit the Windows PowerShell ScriptCenter at:&#160; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dustin Marx has a blog entry where he compares Unix\/Linux, PowerShell and DOS commands.&#160; In it he says, &quot;If there is one Unix command I would love to have in PowerShell, it is the grep command with its regular expression support.&quot;&#160; Well Dustin, your wish is our command.&#160; Select-String command to be precise: PS&gt; Get-Help [&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":[189,16],"class_list":["post-6491","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-grep","tag-select-string"],"acf":[],"blog_post_summary":"<p>Dustin Marx has a blog entry where he compares Unix\/Linux, PowerShell and DOS commands.&#160; In it he says, &quot;If there is one Unix command I would love to have in PowerShell, it is the grep command with its regular expression support.&quot;&#160; Well Dustin, your wish is our command.&#160; Select-String command to be precise: PS&gt; Get-Help [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6491","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=6491"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6491\/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=6491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=6491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=6491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}