PowerShell Compare User Info in Multiple Domains

Below is a script to compare a user’s Given Name between two domains.  Useful for a development and production comparison.

##Get Domain Credentials
$domain1Creds = Get-Credential ##Enter credentials for first domain;
$domain2Creds = Get-Credential ##Enter credentials for first domain;

##Get Domain Users
$adUsersD1 = Get-ADUser -Filter * -Server Domain1 -Credential $domain1Creds;
$adUsersD2 = Get-ADUser -Filter * -Server Domain2 -Credential $domain2Creds;

Foreach ( $user in $adUsersD1 ) {
     $userName = $user.SamAccountName;
     $userGivenName = $user.GivenName;

     ##Get Index and entry of user in the adUsersD2 array
     $d2Index = $adUsersD2.SamAccountname.IndexOf($userName);
     $user2Compare = $adUsersD2[$d2Index];

     ##Compare Given name from domain1 to domain2
     if ( $userGivenName -ne $user2Compare.GivenName ) {
          echo “$userName Given name is different in domain2”;
     }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: