Finding Duplicate UPN’s 2 Domains – Powershell

Posted by | No Tags | PowerShell

Hello All

I was recently working on a project to migrate users from legacy domain to a primary domain. One of the many challenges is finding conflicting UPN between the two domains. UPN  (User-Principal-Name) is an Internet-style login name for a user based on the Internet standard RFC 822. The UPN is shorter than the distinguished name and easier to remember. This should map to the user email name.

The script will search the source domain and the target domain looking for the same UPN prefix.  For each user object, we are gathering: SamAccountName, UserPrincipalName, Initials, FirstName, and SN. Each user attribute is assigned S or T  SamAccountName, UserPrincipalName, Initials, FirstName, and SN for matching and exporting.

Depending on if we have an export of the source domain user UPN in csv format, that data can be imported.  Otherwise we could use an mounted PSDrive to gather the data from the source domain when running the script. I will cover New-PSDrive in more detail, in a later blog.

The script uses OUT-File with predefined headers.

$FN_LN_Matcht$FN_LN_I_Matcht$UPN_Matcht$SAM_Matcht$Ssamaccountnamet$Tsamaccountnamet$Suserprincipalnamet$Tuserprincipalnamet$SFirstnamet$TFirstnamet$SInitialst$TInitialst$SLastnamet$Tlastnamet$Tname” | Out-File $UserOutFile -encoding ASCII -append

Now for some code.

The code is searching the source domain via LDAP:  with the filter “USER” object and “UPN@*” where UPN is current user object.

Now lets search the Target domain (Primary).

The $ObjFilter is still defined from above.

Additional code is used to match the records.


That’s about it for now!

Consultant – Model Technology Solutions
Chris has more than 20 years of industry experience. Prior to his work with Model, Chris worked for one of St. Louis’ largest law firms where his responsibilities ranged from implementing Citrix to automating desktop deployments to virtualizing the datacenter. He has also worked with Microsoft’s Rapid Deployment Program to bring new technologies to one of the three largest consumer credit reporting agencies in the US.