Search code examples
powershellactive-directorycomparison

Compare-Object on two AD user accounts


Can anyone please give some advice on comparing two ADAccount Objects in PowerShell (v2).

Whenever I run a comparison with Compare-Object, it only shows the difference in the Distinguished name, not the differences in the fields of those accounts.

Short of separately comparing $ADUser.Modified , or $ADUser.DisplayName , etc for every field I want to check, I'm at a loss.

Is there a way to compare each and every field in the ADUser object across the two accounts, showing which fields are different?

(If you are curious... this is actually for comparing two accounts with the same name, but in different domains)

Thanks in advance.


Solution

  • This should give you the property name, what each user had as that property and if it was equal or different.

    $user1 = get-aduser Test.User1 -Properties *
    $user2 = get-aduser Test.User2 -Properties *
    
    $Usercomparison = @()
    
    $user1.GetEnumerator() | ForEach-Object {
        If ($User2.($_.Key) -eq $_.Value)
        {
            $Comparison = 'Equal'
        }
        else
        {
            $Comparison = 'Different'
        }
    
        $UserObj = New-Object PSObject -Property ([ordered]@{
            Property = $_.Key
            User1 = $_.Value
            User2 = $User2.($_.Key)
            Comparison = $Comparison
        })
        $UserComparison += $UserObj
    }
    
    $UserComparison