Active Directory Naming Conversion using Powershell
When dealing with Exchange, most of the object Identities are in the form of CanonicalName which makes searching Active Directory difficult since Canonical Name is a calculated attribute and cannot be used in a query.
For example, the identity of a mailbox is the cn of the user:
The newly released version of my Active Directory module (1.3.0) contains the cmdlet "Convert-ActiveDirectoryNaming" that will help with converting between the different Active Directory naming formats. The following example illustrates the usage of the cmdlet.
First, let's get the user from Active Directory in order to have the DistinguishedName, SamAccountName, CanonicalName and UserPrincipalName values.
Now we're going to use the "Convert-ActiveDirectoryNaming" cmdlet to get the different naming formats for the above user.
Have fun!
For example, the identity of a mailbox is the cn of the user:
[PS]
C:\Windows\system32>$cn = (get-mailbox cpolydorou).Identity.ToString()
[PS] C:\Windows\system32>$cn
LAB.local/LAB/Users/Christos Polydorou
[PS] C:\Windows\system32>$cn
LAB.local/LAB/Users/Christos Polydorou
The newly released version of my Active Directory module (1.3.0) contains the cmdlet "Convert-ActiveDirectoryNaming" that will help with converting between the different Active Directory naming formats. The following example illustrates the usage of the cmdlet.
First, let's get the user from Active Directory in order to have the DistinguishedName, SamAccountName, CanonicalName and UserPrincipalName values.
PS C:\> $user = Get-ADUser cpolydorou -Properties DistinguishedName, SamAccountName, CanonicalName, UserPrincipalName
PS C:\> $user
CanonicalName : LAB.local/LAB/Users/Christos Polydorou
DistinguishedName : CN=Christos Polydorou,OU=Users,OU=LAB,DC=LAB,DC=local
Enabled : True
GivenName : Christos
Name : Christos Polydorou
ObjectClass : user
ObjectGUID : 241a6ed9-832e-458b-ab77-c812c0cc57ec
SamAccountName : cpolydorou
SID : S-1-5-21-2384431003-3405447889-4141679475-1117
Surname : Polydorou
UserPrincipalName : cpolydorou@LAB.local
PS C:\>
PS C:\> $user
CanonicalName : LAB.local/LAB/Users/Christos Polydorou
DistinguishedName : CN=Christos Polydorou,OU=Users,OU=LAB,DC=LAB,DC=local
Enabled : True
GivenName : Christos
Name : Christos Polydorou
ObjectClass : user
ObjectGUID : 241a6ed9-832e-458b-ab77-c812c0cc57ec
SamAccountName : cpolydorou
SID : S-1-5-21-2384431003-3405447889-4141679475-1117
Surname : Polydorou
UserPrincipalName : cpolydorou@LAB.local
PS C:\>
Now we're going to use the "Convert-ActiveDirectoryNaming" cmdlet to get the different naming formats for the above user.
PS C:\> Convert-ActiveDirectoryNaming -DN $user.DistinguishedName | fl
DN : CN=Christos Polydorou,OU=Users,OU=LAB,DC=LAB,DC=local
CN : LAB.local/LAB/Users/Christos Polydorou
UPN : cpolydorou@LAB.local
NT : LAB\cpolydorou
PS C:\> Convert-ActiveDirectoryNaming -CN $user.CanonicalName | fl
DN : CN=Christos Polydorou,OU=Users,OU=LAB,DC=LAB,DC=local
CN : LAB.local/LAB/Users/Christos Polydorou
UPN : cpolydorou@LAB.local
NT : LAB\cpolydorou
PS C:\> Convert-ActiveDirectoryNaming -NT "LAB\CPolydorou" | fl
DN : CN=Christos Polydorou,OU=Users,OU=LAB,DC=LAB,DC=local
CN : LAB.local/LAB/Users/Christos Polydorou
UPN : cpolydorou@LAB.local
NT : LAB\CPolydorou
PS C:\> Convert-ActiveDirectoryNaming -UPN $user.UserPrincipalName | fl
DN : CN=Christos Polydorou,OU=Users,OU=LAB,DC=LAB,DC=local
CN : LAB.local/LAB/Users/Christos Polydorou
UPN : cpolydorou@LAB.local
NT : LAB\cpolydorou
PS C:\>
Have fun!