Using the Quest Active Directory Roles to generate a unique UserPrincipalName address.

Here is a neat tip for using the Quest Active Directory Roles to generate a unique UserPrincipalName (UPN) address. You can use this to help generate a unique Alias, UPN and Email address when automating new user accounts.


$Name = "FirstName.LastName" #First and last name of the user you would like to create
$defaultname = $name

$Exit = 0
$Count = 1


Do
{
Try
{
# Checks for all UserPrincipalName addresses which match $name@contoso.com
$User =Get-QADUser -UserPrincipalName "$Name@contoso.com"
If ($User -eq $null) {$Exit = 1}
else
{
# The UserPrincipalName exists, add +1 to count
$Name = $defaultname + $Count++
}
}
Catch
{
# UserPrincipalName does not exist. got to exit.

$Exit = 1
}
}


While ($Exit -eq 0)

$UPN = $name
$UPN

Leave a Reply

Your email address will not be published. Required fields are marked *