title: How to stop pipeline filtering (Where-Object) on first match
body: <p>I searched but didn't find how to do it yet.<br>
  I am working on filtering data from large files (~2GB).<br>
  I used <code>Where-Object</code> and when it find match it continues to search for other matches which it makes sense.  </p>
  
  <p>Is it possible to stop it on the first match ?  </p>
  
  <p>For example <strong>(#1)</strong>:  </p>
  
  <pre><code>Get-Process | Where-Object {$_.ProcessName.StartsWith("svchost")}
  </code></pre>
  
  <p>The output will be:  </p>
  
  <pre><code>Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
  -------  ------    -----      ----- -----   ------     -- -----------
      666      38    26928      18672    92             568 svchost
      596      28    11516      16560    92             792 svchost
      425      14     5364       7036    45             832 svchost
      406      17     7032       8416    39            1004 svchost
  </code></pre>
  
  <p><strong>What I want</strong> is to return the output after the first match:  </p>
  
  <pre><code>Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
  -------  ------    -----      ----- -----   ------     -- -----------
      666      38    26928      18672    92             568 svchost
  </code></pre>
  
  <p>This is what I tried (also with Foreach-Object):  </p>
  
  <pre><code>Get-Process | Where-Object {if($_.ProcessName.StartsWith("svchost")){return $_}}
  Get-Process | Where-Object {if($_.ProcessName.StartsWith("svchost")){return $_;break;}}    
  Get-Process | ForEach-Object {if($_.ProcessName.StartsWith("svchost")){return $_}}
  </code></pre>
  
  <p>But it still returns the full output.<br>
  Reference:<br>
  <a href="https://stackoverflow.com/questions/25911180/how-to-break-foreach-loop-in-powershell">How to break Foreach loop in Powershell?</a><br>
  <a href="https://stackoverflow.com/questions/278539/is-it-possible-to-terminate-or-stop-a-powershell-pipeline-from-within-a-filter">Is it possible to terminate or stop a PowerShell pipeline from within a filter</a></p>
  
  <p><strong>EDIT (explanation about the problem with large data):</strong><br>
  <strong>Example (#2):</strong><br>
  I have two XMLs:<br>
  <strong>A.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Events&gt;
    &lt;Event&gt;
      &lt;EventData Name="Time"&gt;09/10/2017 12:54:16&lt;/EventData&gt;
      &lt;EventData Name="WorkstationName"&gt;USER2-PC&lt;/EventData&gt;
      &lt;EventData Name="UserName"&gt;user2&lt;/EventData&gt;
    &lt;/Event&gt;  
  &lt;/Events&gt;
  </code></pre>
  
  <p><strong>B.xml:</strong>  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Events&gt;
     &lt;Event&gt;
      &lt;EventData Name="Time"&gt;09/10/2017 14:54:16&lt;/EventData&gt;
      &lt;EventData Name="WorkstationName"&gt;USER1-PC&lt;/EventData&gt;
      &lt;EventData Name="UserName"&gt;user1&lt;/EventData&gt;
    &lt;/Event&gt;
    &lt;Event&gt;
      &lt;EventData Name="Time"&gt;09/10/2017 13:54:16&lt;/EventData&gt;
      &lt;EventData Name="WorkstationName"&gt;USER2-PC&lt;/EventData&gt;
      &lt;EventData Name="UserName"&gt;user2&lt;/EventData&gt;
    &lt;/Event&gt; 
   ... (more 100,000 events like the above two)
  &lt;/Events&gt;
  </code></pre>
  
  <p>These XMLs are being loaded as objects:  </p>
  
  <pre><code>$fileA = "C:\tmp\A.xml"
  $a = New-Object Xml.XmlDocument
  $a.Load($fileA)
  
  $fileB = "C:\tmp\B.xml"
  $b = New-Object Xml.XmlDocument
  $b.Load($fileB)
  </code></pre>
  
  <p>Then I want to search for the first match of the same username:  </p>
  
  <pre><code>$result = $b.Events.Event | Where-Object {
      (($_.EventData | where-object {$_.Name -eq "UserName"})."#text" -eq $username)
  }
  
  $result.EventData
  </code></pre>
  
  <p>In this case it waste of time to run over the rest of 99,999 events if I have match on the first event.  </p>
  
  <p><strong>EDIT (SOLVED):</strong><br>
  After reading Nick answer, there wasn't anything new I didn't try.<br>
  The command:  </p>
  
  <pre><code>Get-Process | Where-Object {if($_.ProcessName.StartsWith("svchost")){ $_;break;}}  
  </code></pre>
  
  <p>Indeed stops the <code>Where-Object</code> but it doesn't return the item.<br>
  This one can be solved by:  </p>
  
  <pre><code>Get-Process | Where-Object {if($_.ProcessName.StartsWith("svchost")){ $someVar = $_;break;}}  
  </code></pre>
  
  <p>Therefore I marked his answer.  </p>