title: How to handle $null in the pipeline
body: <p>I often have the following situation in my PowerShell code: I have a function or property that returns a collection of objects, or <code>$null</code>. If you push the results into the pipeline, you also handle an element in the pipeline if <code>$null</code> is the only element.</p>
  
  <p>Example: </p>
  
  <pre><code>$Project.Features | Foreach-Object { Write-Host "Feature name: $($_.Name)" }
  </code></pre>
  
  <p>If there are no features ($Project.Features returns $null), you will see a single line with "Feature name:".</p>
  
  <p>I see three ways to solve this:</p>
  
  <pre><code>if ($Project.Features -ne $null)
  {
    $Project.Features | Foreach-Object { Write-Host "Feature name: $($_.Name)" }
  }
  </code></pre>
  
  <p>or </p>
  
  <pre><code>$Project.Features | Where-Object {$_ -ne $null) | Foreach-Object { 
    Write-Host "Feature name: $($_.Name)" 
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>$Project.Features | Foreach-Object { 
    if ($_ -ne $null) {
      Write-Host "Feature name: $($_.Name)" }
    }
  }
  </code></pre>
  
  <p>But actually I don't like any of these approaches, but what do you see as the best approach?</p>