title: Powershell Try catch write errors to an external txt file
body: <p>I have a PowerShell script to disable email forwarding in Office 365 using a CSV file. I want to catch all errors to an external Txt file.
  Here's my code:</p>
  
  <pre><code>$Groups |
  ForEach-Object {
      $PrimarySmtpAddress = $_.PrimarySmtpAddress
      try {
          # disable Mail forwarding
          Set-Mailbox -Identity $PrimarySmtpAddress -ForwardingSmtpAddress $Null 
      }
      Catch {
          $PrimarySmtpAddress  | Out-File $logfile -Append
      }
  }
  </code></pre>
  
  <p>but it doesn't catch errors.</p>
  
  <p>Is there any instruction to catch errors to an external file?</p>
  
  <p>Any solution will be helpful.</p>