title: Is it possible to pipe conditionally in Powershell, i.e. execute an element of a pipeline only if a condition is met?
body: <p>I want to do something like this:</p>
  
  <pre><code>&lt;statement&gt; | &lt;filter1&gt; | &lt;filter2&gt; if &lt;condition&gt; | &lt;filter3&gt; | &lt;filter4&gt; | &lt;filter5&gt;
  </code></pre>
  
  <p>The results of &lt;statement&gt; run through &lt;filter1&gt;, then they run through &lt;filter2&gt; only if &lt;condition&gt; is met, then through the remaining filters regardless of whether &lt;filter2&gt; was applied. This is the equivalent of:</p>
  
  <pre><code>if (&lt;condition&gt;) {
    &lt;statement&gt; | &lt;filter1&gt; | &lt;filter2&gt; | &lt;filter3&gt; | &lt;filter4&gt; | &lt;filter5&gt;
  } else {
    &lt;statement&gt; | &lt;filter1&gt; | &lt;filter3&gt; | &lt;filter4&gt; | &lt;filter5&gt;
  }
  </code></pre>
  
  <p>This would be useful in functions where a given filter is applied to the result set only if a certain switch was invoked. If the conditional filter occurs early in a long pipeline, writing it with an outer if-block results in a lot of repetition of code, especially if there is more than one conditional filter.</p>
  
  <p>Here's an example. The following function shows the permissions a given account has in a given directory subtree (e.g. <code>Show-AccountPerms \\SERVERX\Marketing DOMAIN\jdoe</code> gives a report of permissions that the user DOMAIN\jdoe has in the directory tree under \SERVERX\Marketing).</p>
  
  <pre><code>function Show-AccountPerms {
      param (
          [parameter(mandatory = $true)]$rootdir,
          [parameter(mandatory = $true)]$account,
          [switch]$files,
          [switch]$inherited
      )
      gci -r $rootdir `
      |where {$_.psiscontainer} `
      |foreach {
          $dir = $_.fullname
          (get-acl $_.pspath).access `
          | where {$_.isinherited -eq 'False'} `
          |foreach {
              if ($_.identityreference -eq $account) {
                  "{0,-25}{1,-35}{2}" -f $_.identityreference, $_.filesystemrights, $dir
              }
          }
      }
  }
  </code></pre>
  
  <p>By default, it only shows explicit permissions (enforced by the <code>| where {$_.isinherited -eq 'False'}</code> filter), and only on directories (enforced by the <code>|where {$_.psiscontainer}</code> filter).</p>
  
  <p>However, I want to ignore <code>|where {$_.psiscontainer}</code> if the -files switch is invoked, and ignore <code>| where {$_.isinherited -eq 'False'}</code> if the -inherited switch is invoked. Accomplishing this with outer if blocks would quadruple the code, and almost 75% of it would be repetition. Is there a way to keep these filters in-line but instruct powershell to only apply them of the corresponding switch is false?</p>
  
  <p>Please note that this is just an <em>example</em>, so I'm not interested in any workarounds specific to this function. I'm looking for an answer to my general question regarding piping conditionally, not a solution for how to accomplish this particular task.</p>