{"id":2991,"date":"2010-05-06T20:41:12","date_gmt":"2010-05-06T20:41:12","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2010\/05\/06\/select-string-context\/"},"modified":"2019-02-18T13:05:56","modified_gmt":"2019-02-18T20:05:56","slug":"select-string-context","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/select-string-context\/","title":{"rendered":"Select-String -Context"},"content":{"rendered":"<p>A lot of people use Select-String but I haven\u2019t seen much discussion of the \u2013Context parameter.&#160;&#160; This is an awesome feature that we added in V2.&#160; If you haven\u2019t tried it, you should spend a few minutes experimenting.&#160; I can guarantee you that will you be glad you did.<\/p>\n<p>Let\u2019s see what the HELP has to say about the Context parameter:<\/p>\n<p>&#160;<\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">PS&gt; Get-Help Select-String -Parameter Context <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">-Context &lt;Int32[]&gt;     <br \/>&#160;&#160;&#160; Captures the specified number of lines before and after the line with the m      <br \/>&#160;&#160;&#160; atch. This allows you to view the match in context. <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160; If you enter one number as the value of this parameter, that number determi     <br \/>&#160;&#160;&#160; nes the number of lines captured before and after the match. If you enter t      <br \/>&#160;&#160;&#160; wo numbers as the value, the first number determines the number of lines be      <br \/>&#160;&#160;&#160; fore the match and the second number determines the number of lines after t      <br \/>&#160;&#160;&#160; he match. <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160; In the default display, lines with a match are indicated by a right angle b     <br \/>&#160;&#160;&#160; racket (ASCII 62) in the first column of the display. Unmarked lines are th      <br \/>&#160;&#160;&#160; e context. <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160; This parameter does not change the number of objects generated by Select-St     <br \/>&#160;&#160;&#160; ring. Select-String generates one MatchInfo (Microsoft.PowerShell.Commands.      <br \/>&#160;&#160;&#160; MatchInfo) object for each match. The context is stored as an array of stri      <br \/>&#160;&#160;&#160; ngs in the Context property of the object. <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160; When you pipe the output of a Select-String command to another Select-Strin     <br \/>&#160;&#160;&#160; g command, the receiving command searches only the text in the matched line      <br \/>&#160;&#160;&#160;&#160; (the value of the Line property of the MatchInfo object), not the text in      <br \/>&#160;&#160;&#160; the context lines. As a result, the Context parameter is not valid on the r      <br \/>&#160;&#160;&#160; eceiving Select-String command. <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160; When the context includes a match, the MatchInfo object for each match incl     <br \/>&#160;&#160;&#160; udes all of the context lines, but the overlapping lines appear only once i      <br \/>&#160;&#160;&#160; n the display. <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&#160;&#160;&#160; Required?&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; false     <br \/>&#160;&#160;&#160; Position?&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; named      <br \/>&#160;&#160;&#160; Default value      <br \/>&#160;&#160;&#160; Accept pipeline input?&#160;&#160;&#160;&#160;&#160;&#160; false      <br \/>&#160;&#160;&#160; Accept wildcard characters?&#160; false<\/font><\/p>\n<p>&#160;<\/p>\n<p>Let\u2019s have some fun.&#160; I\u2019m going to use Superstar James O\u2019Neil\u2019s <a href=\"http:\/\/pshyperv.codeplex.com\/\">Hypervisor Management Library<\/a> to experiment with.&#160; He has a number of functions the work with NICs.&#160; Let\u2019s party.<\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">PS&gt; #First Let&#8217;s find the functions     <br \/>PS&gt; <strong>dir *.ps1 |Select-String function.*NIC<\/strong> <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">network.ps1:3:Function Add-VMNIC     <br \/>network.ps1:64:Function Get-VMNic      <br \/>network.ps1:94:Function Get-VMNicport      <br \/>network.ps1:104:Function Get-VMnicSwitch      <br \/>network.ps1:121:Function Get-VMNICVLAN      <br \/>network.ps1:282:Function Remove-VMNIC      <br \/>network.ps1:332:Function remove-VMSwitchNIC      <br \/>network.ps1:366:Function Select-VMNIC      <br \/>network.ps1:392:Function Set-VMNICAddress      <br \/>network.ps1:422:Function Set-VMNICSwitch      <br \/>network.ps1:459:Function Set-VMNICVLAN <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\"><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\"><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">PS&gt; <strong># That gave me too many so let&#8217;s add a $ to the end of the SEARCH STRING<\/strong>      <br \/>PS&gt; <strong># That indicates a Line end       <br \/><\/strong>PS&gt; <strong>dir *.ps1 |Select-String function.*NIC<font color=\"#ff0000\">$ <\/font><\/strong><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">network.ps1:3:Function Add-VMNIC     <br \/>network.ps1:64:Function Get-VMNic      <br \/>network.ps1:282:Function Remove-VMNIC      <br \/>network.ps1:366:Function Select-VMNIC<\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\"><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\"><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">PS&gt; <strong># Let&#8217;s see 1 line before and after each matching line       <br \/><\/strong>PS&gt; <strong>dir *.ps1 |Select-String function.*NIC$ -Context 1 <\/strong><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&#160; network.ps1:<font color=\"#ff0000\">2<\/font>:      <br \/><font color=\"#ff0000\">&gt;<\/font> network.ps1:<font color=\"#ff0000\">3<\/font>:Function Add-VMNIC      <br \/>&#160; network.ps1:<font color=\"#ff0000\">4<\/font>:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:63:      <br \/>&gt; network.ps1:64:Function Get-VMNic      <br \/>&#160; network.ps1:65:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:281:      <br \/>&gt; network.ps1:282:Function Remove-VMNIC      <br \/>&#160; network.ps1:283:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:365:      <br \/>&gt; network.ps1:366:Function Select-VMNIC      <br \/>&#160; network.ps1:367:{# .ExternalHelp&#160; MAML-VMNetwork.XML <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">PS&gt; <strong># Notice the line numbers and that the matching line has a &gt; in front<\/strong><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\"><strong><\/strong><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\"><strong><\/strong>      <\/p>\n<p><\/p>\n<p>   <\/font><font color=\"#000080\" size=\"2\" face=\"Courier New\">PS&gt;<strong> # Now let&#8217;s try 3 lines before and after the matching line       <br \/><\/strong>PS&gt; <strong>dir *.ps1 |Select-String function.*NIC$ -Context 3 <\/strong><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&#160; network.ps1:1:     <br \/>&#160; network.ps1:2:      <br \/>&gt; network.ps1:3:Function Add-VMNIC      <br \/>&#160; network.ps1:4:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:5:&#160;&#160;&#160; [CmdletBinding(SupportsShouldProcess=$true)]      <br \/>&#160; network.ps1:6:&#160;&#160;&#160; param(      <br \/>&#160; network.ps1:61:}      <br \/>&#160; network.ps1:62:      <br \/>&#160; network.ps1:63:      <br \/>&gt; network.ps1:64:Function Get-VMNic      <br \/>&#160; network.ps1:65:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:66:&#160;&#160;&#160; param(      <br \/>&#160; network.ps1:67:&#160;&#160;&#160;&#160;&#160;&#160;&#160; [parameter(ValueFromPipeline = $true)]      <br \/>&#160; network.ps1:279:}      <br \/>&#160; network.ps1:280:      <br \/>&#160; network.ps1:281:      <br \/>&gt; network.ps1:282:Function Remove-VMNIC      <br \/>&#160; network.ps1:283:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:284:&#160;&#160;&#160; [CmdletBinding(SupportsShouldProcess=$true)]      <br \/>&#160; network.ps1:285:&#160;&#160;&#160; param(      <br \/>&#160; network.ps1:363:      <br \/>&#160; network.ps1:364:      <br \/>&#160; network.ps1:365:      <br \/>&gt; network.ps1:366:Function Select-VMNIC      <br \/>&#160; network.ps1:367:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:368:&#160;&#160;&#160; param(      <br \/>&#160; network.ps1:369:&#160;&#160;&#160;&#160;&#160;&#160;&#160; [parameter(ValueFromPipeline = $true)]<\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\"><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\"><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">PS&gt; <strong># The line before is boring so let&#8217;s get rid of that and then see a       <br \/><\/strong>PS&gt; <strong># few lines after       <br \/><\/strong>PS&gt; <strong>dir *.ps1 |Select-String function.*NIC$ -Context 0,3<\/strong> <\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&gt; network.ps1:3:Function Add-VMNIC     <br \/>&#160; network.ps1:4:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:5:&#160;&#160;&#160; [CmdletBinding(SupportsShouldProcess=$true)]      <br \/>&#160; network.ps1:6:&#160;&#160;&#160; param(      <br \/>&gt; network.ps1:64:Function Get-VMNic      <br \/>&#160; network.ps1:65:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:66:&#160;&#160;&#160; param(      <br \/>&#160; network.ps1:67:&#160;&#160;&#160;&#160;&#160;&#160;&#160; [parameter(ValueFromPipeline = $true)]      <br \/>&gt; network.ps1:282:Function Remove-VMNIC      <br \/>&#160; network.ps1:283:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:284:&#160;&#160;&#160; [CmdletBinding(SupportsShouldProcess=$true)]      <br \/>&#160; network.ps1:285:&#160;&#160;&#160; param(      <br \/>&gt; network.ps1:366:Function Select-VMNIC      <br \/>&#160; network.ps1:367:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:368:&#160;&#160;&#160; param(      <br \/>&#160; network.ps1:369:&#160;&#160;&#160;&#160;&#160;&#160;&#160; [parameter(ValueFromPipeline = $true)]<\/font><\/p>\n<p>&#160;<\/p>\n<p>&#160;<\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">PS&gt; <strong># Let&#8217;s take a look at the body of Remove-VMNIC       <br \/><\/strong>PS&gt; <strong>dir *.ps1 |Select-String function.*Remove-VMNIC$ -Context 0,20 <\/strong><\/font><\/p>\n<p><font color=\"#000080\" size=\"2\" face=\"Courier New\">&gt; network.ps1:282:Function Remove-VMNIC     <br \/>&#160; network.ps1:283:{# .ExternalHelp&#160; MAML-VMNetwork.XML      <br \/>&#160; network.ps1:284:&#160;&#160;&#160; [CmdletBinding(SupportsShouldProcess=$true)]      <br \/>&#160; network.ps1:285:&#160;&#160;&#160; param(      <br \/>&#160; network.ps1:286:&#160;&#160;&#160;&#160;&#160;&#160;&#160; [parameter(Mandatory = $true ,&#160; ValueFromPipeline = $true)]      <br \/>&#160; network.ps1:287:&#160;&#160;&#160;&#160;&#160;&#160;&#160; $Nic,      <br \/>&#160; network.ps1:288:      <br \/>&#160; network.ps1:289:&#160;&#160;&#160;&#160;&#160;&#160;&#160; $PSC,      <br \/>&#160; network.ps1:290:&#160;&#160;&#160;&#160;&#160;&#160;&#160; [switch]$force,      <br \/>&#160; network.ps1:291:      <br \/>&#160; network.ps1:292:&#160;&#160;&#160;&#160;&#160;&#160;&#160; $VM, $Server #VM no longer required, but preserved for compatibility with V1      <br \/>&#160; network.ps1:293:&#160;&#160;&#160; )      <br \/>&#160; network.ps1:294:&#160;&#160;&#160; process {      <br \/>&#160; network.ps1:295:&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ($psc -eq $null)&#160; {$psc = $pscmdlet} ; if (-not $PSBoundParameters.psc) {$PSBoundParameter      <br \/>s.add(&quot;psc&quot;,$psc)}      <br \/>&#160; network.ps1:296:&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ($NIC -is [Array] ) {[Void]$PSBoundParameters.Remove(&quot;NIC&quot;) ;&#160; $NIC | ForEach-object {Remo      <br \/>ve-VMNIC&#160; -NIC $_&#160; @PSBoundParameters}}      <br \/>&#160; network.ps1:297:&#160;&#160;&#160;&#160;&#160;&#160;&#160; if ($nic -is [System.Management.ManagementObject])&#160; {remove-VMRasd -rasd $NIC -PSC $psc }      <br \/>&#160; network.ps1:298:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; # note: In V1 the switch port was removed before removing the NIC, but this is done autom      <br \/>atically.      <br \/>&#160; network.ps1:299:&#160;&#160;&#160; }      <br \/>&#160; network.ps1:300:}      <br \/>&#160; network.ps1:301:      <br \/>&#160; network.ps1:302:<\/font><\/p>\n<p>&#160;<\/p>\n<p>Party on your own.&#160; You are going to love \u2013Context!<\/p>\n<p>Experiment!&#160; Enjoy!&#160; Engage! <\/p>\n<p>Jeffrey Snover [MSFT]   <br \/>Distinguished Engineer    <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>A lot of people use Select-String but I haven\u2019t seen much discussion of the \u2013Context parameter.&#160;&#160; This is an awesome feature that we added in V2.&#160; If you haven\u2019t tried it, you should spend a few minutes experimenting.&#160; I can guarantee you that will you be glad you did. Let\u2019s see what the HELP has [&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":[],"class_list":["post-2991","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>A lot of people use Select-String but I haven\u2019t seen much discussion of the \u2013Context parameter.&#160;&#160; This is an awesome feature that we added in V2.&#160; If you haven\u2019t tried it, you should spend a few minutes experimenting.&#160; I can guarantee you that will you be glad you did. Let\u2019s see what the HELP has [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/2991","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=2991"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/2991\/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=2991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=2991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=2991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}