title: Suppress Powershell Errors while adding to $Error Variable
body: <p>I have a block of code executing that has been set up with a simple If/Else block to catch errors. I would normally use Try/Catch but the Exchange 2010 PS environment I'm working in doesn't allow me to use most of the Try/Catch features (and I can't update it or change it in any way because it's the customer's system and they're unwilling). </p>
  
  <p>The issue is that while the code will function as expected when the Add-DistributionGroupMember cmdlet is set with -ErrorAction "Stop", it will output the error to the host each time, which annoys the customer (and me) because it's effectively just red noise since all the possible errors are being handled via a detailed output file.</p>
  
  <p>If I set the cmdlet to -ErrorAction "SilentlyContinue" the error text is suppressed, but the Error is not added to the $Error[0] spot as I expected. The same is true of -ErrorAction "Ignore". The code requires that the Error be added to the $Error variable each time there is an error.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>$ListMembershipsIn | % {
  
          $Alias = $_.Alias
          $Member = $_.Member
  
          Add-DistributionGroupMember -Identity $Alias -Member $Member -Confirm:$false -ErrorAction Stop
  
  
          if($Error[0] -match "The recipient"){
              Write-Host -ForegroundColor Yellow "Already a member"
              Add-Content -Path $OutputPath -Value "$($Alias),$($Member),Group already contains Member"
          }
          elseif($Error[0] -match "couldn't be found"){
              Write-Host -ForegroundColor Yellow "not found"
              Add-Content -Path $OutputPath -Value "Group does not exist or cannot be found,$($Alias),N/A"
          }
          elseif($Error[0] -match "couldn't find"){
              Write-Host -ForegroundColor Yellow "not found"
              Add-Content -Path $OutputPath -Value "Member does not exist or cannot be found,$($Alias),$($Member)"
          }
          elseif($Error[0] -match "There are Multiple"){
              Add-Content -Path $OuputPath -Value "Member name matches too many recipient - Add Member Manually,$($Alias),$($Member)"
          }
          else{
              Add-Content -Path $OutputPath -Value "Member Successfully Added to Group,$($Alias),$($Member)"
              Write-Host -ForegroundColor Green "Throw Flag here"
          }
      }
  </code></pre>