Writing attribute changes to an output file

Hi All,

Hoping to get some guidance with this. The script works fine, but I would like to know if there is anyway to output before and after changes.

Import-Csv .\adusers6.csv |ForEach-Object { Set-ADUser $_.SamAccountName -Replace @{
  Department = $_.Department
  departmentNumber = $_.departmentNumber
  StreetAddress = $_.StreetAddress
  Description = $_.Description
  physicalDeliveryOfficeName = $_.physicalDeliveryOfficeName
  DisplayName = $_.DisplayName
 }
}

you have to log things before and after changes.

Write to a file or print in console before using Set-AdUser and use -PassThru parameter of Set-AdUser and capture the output in a variable, then print or write to a file using the output variable.

Hi kvprasoon,

My POSH skills don’t go that far unfortunately, could you give me a hint of where I would pace this in the script?

Like this

Import-Csv .\adusers6.csv | ForEach-Object {
$Before = Get-AdUser -Filter "SamAccountName -eq $($_.SamAccountName)" -Properties physicalDeliveryOfficeName,StreetAddress,Department,departmentNumber

$Before | Out-String | Out-File -FilePath <path to log file>

$After = Set-ADUser $_.SamAccountName -PassThru -Replace @{
  Department = $_.Department
  departmentNumber = $_.departmentNumber
  StreetAddress = $_.StreetAddress
  Description = $_.Description
  physicalDeliveryOfficeName = $_.physicalDeliveryOfficeName
  DisplayName = $_.DisplayName
 }
$After | Select-Object -Property DisplayName,Description,physicalDeliveryOfficeName,StreetAddress,Department,departmentNumber | Out-String | Out-File -FilePath <log file path> -Append
}

Thank you so much, this worked brilliantly for me.

Thank you, thank you, thank you… a million time, so much appreciated