title: How to catch errors from powershell and write them on a .txt file?
body: <p>I am using a script to update the data of Active Directory users</p>
  
  <pre><code>Import-Csv -Path $Path | foreach-object {Set-ADUser -Identity ($_.user) -Department $_.Department -Title $_.Title -Company $_.Company -MobilePhone $_.MobilePhone ...etc (the required data to modify)}
  </code></pre>
  
  <p>I want the script to write the errors in the update of an "x" user's, to see whose user wasn't updated.</p>
  
  <p>I'm creating a C# app to write a personalized script, and still need to make the error log.</p>
  
  <p>The idea is to create this log in a specified path. I don't know if this can be made in the script it self.</p>
  
  <p>I'm using this PowerShell method I have found here:</p>
  
  <pre><code>PowerShell ps = PowerShell.Create();
  ps.AddScript(script);
  
  IAsyncResult result = ps.BeginInvoke();
  
  // do something else until execution has completed.
  // this could be sleep/wait, or perhaps some other work
  while (result.IsCompleted == false)
  {
    //think i can make that here, but can´t see how
  }   
  MessageBox.Show("Complete");
  </code></pre>