{"id":13141,"date":"2011-08-01T00:01:00","date_gmt":"2011-08-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/08\/01\/use-the-powershell-grep-command-to-parse-the-command-line\/"},"modified":"2011-08-01T00:01:00","modified_gmt":"2011-08-01T00:01:00","slug":"use-the-powershell-grep-command-to-parse-the-command-line","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-the-powershell-grep-command-to-parse-the-command-line\/","title":{"rendered":"Use the PowerShell Grep Command to Parse the Command Line"},"content":{"rendered":"<p><strong>Summary<\/strong>: In this article, Microsoft Scripting Guy Ed Wilson teaches how to use the Windows PowerShell version of grep to parse the command line.<\/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 have enjoyed reading Sean&rsquo;s <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=powershell%20legacy&amp;sections=7618\">legacy scripting<\/a> articles, but I am a bit confused. It seems that all these commands return data, and I would love to be able to easily parse the information that the commands return. Can you give me an example of that?<\/p>\n<p>&mdash;KG<\/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 KG,<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. The TechReady conference in Seattle last week was great. We even had a few sun sightings during the week. The day I flew into Seattle was absolutely beautiful, and I was able to see all the mountains stretching up into the sky. The <a href=\"http:\/\/www.bing.com\/attractions\/search?q=Space+Needle%2c+Seattle&amp;qzattrid=w15502&amp;qpvt=seattle+space+needle&amp;FORM=DTPATA\">Space Needle<\/a> was clearly visible. All in all, it was a Seattle Chamber of Commerce type of day.<\/p>\n<p>KG, if you are going to work with legacy commands very often, you will need to become competent at using the <a href=\"http:\/\/blogs.technet.com\/search\/searchresults.aspx?q=Select-String&amp;sections=7618\"><b>Select-String <\/b>cmdlet<\/a>. Often, I will look around for an object that will return the same types of information that I might obtain via a legacy command&mdash;to avoid the issue of extensive text parsing.<\/p>\n<p>For example, the <b>ipconfig<\/b><i> <\/i>command will return an IP address and a log of other information. But I can use WMI to obtain the IP address as well. The following command returns the IP address on my local computer. Note that <b>gwmi<\/b> is an alias for the <b>Get-WMIObject<\/b> cmdlet. I only have one network interface card that is ipenabled, so I can return the <b>ipaddress<\/b> property directly. Unfortunately, the <b>ipaddress<\/b><i> <\/i>property returns an array with both the IPv4 and IPv6 address in it. I index into the array to return only the IPv4 address:<\/p>\n<p style=\"padding-left: 30px\">(gwmi win32_NetworkAdapterConfiguration -Filter &#8216;ipenabled = &#8220;true&#8221;&#8216;).ipaddress[0]<\/p>\n<p>As you can tell, typing the preceding command is a bit cumbersome when compared to typing ipconfig<i>. <\/i>However, if I put the above command in a function and I place it in my profile or some module, I can call it as easily as typing <b>Get-IPAddress<\/b>. Or if I create an alias, I can type <b>gia<\/b>. The <b>Get-IPAddress<\/b> function and code to create an alias are shown here:<\/p>\n<p style=\"padding-left: 30px\"><strong>Get-IPAddressFunction.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\">Function Get-IPAddress<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;(Get-WmiObject -class win32_NetworkAdapterConfiguration `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; -Filter &#8216;ipenabled = &#8220;true&#8221;&#8216;).ipaddress[0]<\/p>\n<p style=\"padding-left: 30px\">} #end function Get-Ipaddress<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">New-Alias -Name GIA -Value Get-IPAddress -Description &#8220;HSG alias&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p>When I run the function to load it into memory, I can then access it by alias if I wish. This is shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8267.hsg-8-1-11-1.jpg\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of accessing function by alias\" alt=\"Image of accessing function by alias\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8267.hsg-8-1-11-1.jpg\" \/><\/a><\/p>\n<p>Figuring out how to use the <b>Select-String<\/b> cmdlet can be a bit confusing. The online help for the cmdlet is rather extensive, and the examples do not seem to cover simple cases. For example, if I want to parse the results of the <b>ipconfig<\/b> command, I might be tempted to do something like this:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Select-String -Pattern &#8220;IPV4&#8221; -InputObject ipconfig<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt;<\/p>\n<p>Unfortunately, nothing is returned. I try putting parentheses around the command to force evaluation first, and I at least see something returned to the screen. The command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Select-String -Pattern &#8220;IPV4&#8221; -InputObject (ipconfig)<\/p>\n<p>The command and output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8461.hsg-8-1-11-2.jpg\"><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\/8461.hsg-8-1-11-2.jpg\" \/><\/a><\/p>\n<p>As can be seen in the preceding figure, the results are completely jumbled and unreadable. It seems that it might be a lost cause, but if you stick with it and use the pipeline, the output is much more readable. The command that uses the pipeline and the associated output are shown here (in addition, it does not matter if I use the <i>simplematch<\/i> switched parameter or not):<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; ipconfig | Select-String -Pattern &#8220;IPv4&#8221; -SimpleMatch<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; IPv4 Address. . . . . . . . . . . : 10.0.0.188<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; ipconfig | Select-String -Pattern &#8220;IPv4&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; IPv4 Address. . . . . . . . . . . : 10.0.0.188<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p>When I try to shorten the command, however, problems arise. I try to use <b>select<\/b> as an alias for the Select-String cmdlet, and no errors appear, but I am left with a blank screen, as shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2376.hsg-8-1-11-3.jpg\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of blank screen\" alt=\"Image of blank screen\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2376.hsg-8-1-11-3.jpg\" \/><\/a><\/p>\n<p>The problem is that <b>select<\/b> is an alias for the <b>Select-Object<\/b> cmdlet, and not for the Select-String cmdlet, as shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Alias select<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"><span style=\"text-decoration: underline\">CommandType<\/span>&nbsp; <span style=\"text-decoration: underline\">Name<\/span>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style=\"text-decoration: underline\">Definition<\/span><\/p>\n<p style=\"padding-left: 30px\">Alias&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Select-Object<\/p>\n<p>&nbsp;<\/p>\n<p>Well, what are the aliases for the <b>Select-String<\/b> cmdlet? According to the output, there isn&rsquo;t even one:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Alias -Definition select-string<\/p>\n<p style=\"padding-left: 30px\">Get-Alias : This command cannot find a matching alias because an alias with definition &#8216;select-string&#8217;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;does not exist.<\/p>\n<p style=\"padding-left: 30px\">At line:1 char:10<\/p>\n<p style=\"padding-left: 30px\">+ Get-Alias &lt;&lt;&lt;&lt;&nbsp; -Definition select-string<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : ObjectNotFound: (select-string:String) [Get-Alias], ItemNotFoundException<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand<\/p>\n<p>Now, I may decide that I would like to create an alias for <b>Select-String<\/b>. <b>Grep<\/b> would be a good one to use. The command to accomplish this appears here (you would want to place it in your startup profile; <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/getting+started\/profiles\/\">see this collection of Hey, Scripting Guy! posts<\/a> for information about profiles):<\/p>\n<p style=\"padding-left: 30px\">New-Alias -Name grep -Value Select-String -Description &#8220;HSG alias&#8221;<\/p>\n<p>When I have an alias for <b>Select-String<\/b>, I can shorten my command quite a bit. The shortened command and associated output are shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; ipconfig | grep &#8220;IPv4&#8221;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; IPv4 Address. . . . . . . . . . . : 10.0.0.188<\/p>\n<p>&nbsp;<\/p>\n<p>KG, this should be enough to get you started playing around with the Select-String cmdlet. Join me again tomorrow, when we will explore some more ways to use this powerful cmdlet.<\/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\">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: In this article, Microsoft Scripting Guy Ed Wilson teaches how to use the Windows PowerShell version of grep to parse the command line. &nbsp; Hey, Scripting Guy! I have enjoyed reading Sean&rsquo;s legacy scripting articles, but I am a bit confused. It seems that all these commands return data, and I would love to [&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":[51,3,4,281,45],"class_list":["post-13141","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-select-string","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: In this article, Microsoft Scripting Guy Ed Wilson teaches how to use the Windows PowerShell version of grep to parse the command line. &nbsp; Hey, Scripting Guy! I have enjoyed reading Sean&rsquo;s legacy scripting articles, but I am a bit confused. It seems that all these commands return data, and I would love to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13141","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=13141"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13141\/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=13141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}