Script repository

Automatically add a digit to the username if it is not unique

Updated on: Jan 18, 2026, Views: 7633

Property validation

The script adds a digit to the username if it is not unique. To execute the script, create a business rule triggering Before creating a user.

Username not limited by length

function IsUserNameUnique($username)
{
   $user = Get-AdmUser $username -erroraction silentlycontinue
   return $user -eq $Null
}

# Get username
$username = $Context.GetModifiedPropertyValue("samAccountName")

# Check if username is unique.
if (IsUserNameUnique($username))
{
    return
}

# Generate unique username.
$uniqueUsername = $Null
for ($i = 1; $True; $i++)
{
    $uniqueUsername = $username + $i;
    if (IsUserNameUnique($uniqueUsername))
    {
        break
    }
}

# Update sAMAccountName
$Context.SetModifiedPropertyValue("samAccountName", $uniqueUsername)

# Update userPrincipalName
$upnSuffix = $Context.GetObjectDomain("%distinguishedName%")
$userLogonName = $uniqueUsername + "@" + $upnSuffix
$Context.SetModifiedPropertyValue("userPrincipalName", $userLogonName)
$Context.LogMessage("The username was changed to " + $userLogonName `
  + ".", "Information")

Username has a length limitation

In the script, the $maximumLength variable specifies the maximum number of characters that a username can have.

$maximumLength = 8 # TODO: modify me

function IsUserNameUnique($username)
{
   $user = Get-AdmUser $username -erroraction silentlycontinue
   return $user -eq $Null
}

# Get username
$username = $Context.GetModifiedPropertyValue("samAccountName")

# Check username length.
if ($username.Length -gt $maximumLength)
{
    $username = $username.SubString(0 , $maximumLength)
}
elseif (IsUserNameUnique($username))
{
    return
}

# Generate unique username.
$uniqueUsername = $username
for ($i = 1; $True; $i++)
{
    if (IsUserNameUnique($uniqueUsername))
    {
        break
    }
    
    $difference = $maximumLength - $username.Length - $i.ToString().Length
    if ($difference -lt 0)
    {
        $username = $username.Substring(0, $username.Length + $difference)
    }
   
    if ([System.String]::IsNullOrEmpty($username))
    {
        $Context.Cancel("Unable to generate a unique username, because the number length exceeds the maximum length of the username")
        return
    }

    $uniqueUsername = $username + $i;
}

# Update sAMAccountName
$Context.SetModifiedPropertyValue("samAccountName", $uniqueUsername)

# Update userPrincipalName
$upnSuffix = $Context.GetObjectDomain("%distinguishedName%")
$userLogonName = $uniqueUsername + "@" + $upnSuffix
$Context.SetModifiedPropertyValue("userPrincipalName", $userLogonName)
$Context.LogMessage("The username was changed to " + $userLogonName `
  + ".", "Information")

Comments 0

You must be signed in to comment.

    Got questions?

    Support Questions & Answers

    We use cookies to improve your experience.
    By your continued use of this site you accept such use.
    For more details please see our privacy policy and cookies policy.