This is a PowerShell script to find duplicate email addresses among any objects in Active Directory. Duplicates raise errors during synchronization with Azure AD or Office 365. All classes of objects are considered. The email addresses can be in any of the following attributes: userPrincipalName, mail, proxyAddresses, and msRTCSIP-PrimaryUserAddress.

The script first finds all objects in the Active Directory domain with any values assigned to any of the four attributes. Then the script evaluates all of the values. Any prefix, such as "SMTP:", "smtp:", or "sip:" is stripped away. Then the remaining values are compared to those in a hash table. If there is a match, the script outputs the value and the distinguished names of the conflicting objects, with the attribute where the value is found in parentheses. If no match is found, the value is added to the hash table.

For example, duplicates could result in output similar to the following:

Duplicate value: jsmith@MyDomain.com
    CN=Jim Smith,OU=West,DC=MyDomain,DC=com (proxyAddresses)
    CN=Jane Smith,OU=East,DC=MyDomain,DC=com (UPN)
Duplicate value: rwilson@MyDomain.com
    CN=Ronald Wilson,OU=West,DC=MyDomain,DC=com (mail, UPN)
    CN=Raymond Wilson,OU=North,DC=MyDomain,DC=com (UPN)

If the duplicate appears in more than one attribute for an object, then both attributes are noted. The output can be redirected to a text file. It then is up to an administrator to correct the conflicts. The output has enough information to locate the conflicting objects, and the attributes with the duplicate values.

FindDuplIDs.txt <<-- Click here to view or download the program