title: Powershell: Outputing Sucess and Errors
body: <p>I have written a very simple Powershell script for bulk renaming AD Groups, below is the code</p>
  
  <pre><code># This script will perform a bulk rename of AD groups.
  # Takes input from a csv file called "ADGroups.csv" with two columns, 
  # first column labelled "ObjectGUID" and contains the GUID of the AD Group you wish to rename and
  # the second column is labeled "NewName" and contains the name that you want the group to be renamed to.
  # Outputs results to console and a file called ADGroupBulkRenameLog.txt
  
  $Groups = Import-Csv ADgroups.csv
  
  Foreach ($Group in $Groups)
  {
      $OldName = Get-ADGroup -Identity $Group.ObjectGUID | Select Name
      Rename-ADObject -Identity $Group.ObjectGUID -NewName $Group.NewName 
      Set-ADGroup -Identity $Group.ObjectGUID -SamAccountName $Group.NewName 
      Write-Output ($OldName.Name + " has been renamed to " + $Group.NewName) | Tee-Object ADGroupBulkRenameLog.txt -Append
  }
  </code></pre>
  
  <p>The renaming part works fine but the part i am having trouble with is the output.  The output strings gets written to both file and console, however if an error occurs (e.g. New name already exists) the error is not written to file and the write-output command still runs.</p>
  
  <p>I was wondering if anyone knows a better way of doing this?  The ultimate goal is to have output to console and file saying if a group was successfully renamed or not and continue if it does error.</p>
  
  <p>Thanks in advance!</p>