Find existing UPN in Forest

A userPrincipalName (UPN) must be unique in a Active Directory Forest. To check for an existing UPN use the following lines. The powershell script expects that all DC’s within the Root Domain are GC enabled .

$RootDomain = ""
$DomainControllerRoot = (Get-ADDomainController -Filter * -server $RootDomain | select Hostname).hostname[0]
$GlobalCatalog = $DomainControllerRoot +":3268"
$UserPrincipalName = ""
if ((get-aduser -ldapfilter "(userPrincipalName=$UserPrincipalName)" -server $GlobalCatalog ).userPrincipalName.length -gt 0)
Write-host "UPN NOT unique in Forest : $UserPrincipalName "
Write-host "UPN UNIQUE in Forest : $UserPrincipalName " 
This entry was posted in Information Technology and tagged , , . Bookmark the permalink.

Comments are closed.