Script repository

Automatically add a digit to Full Name if it is not unique in all managed domains

Updated on: Jan 18, 2026, Views: 2342

Property validation

The script automatically adds a digit to Full Name if it is not unique in all managed domains. To execute the script, create a business rule triggering Before creating the required type of objects.

function IsFullNameUnique($fullname)
{
    $searcher = $Context.TargetObject
    $searcher.Criteria = New-AdmCriteria -Type "*" -Expression {cn -eq $fullname}
    $searcher.VirtualRoot = $True
    $searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
    $searcher.SizeLimit = 1
    
    try
    {
        $searchResultIterator = $searcher.ExecuteSearch()
        $searchResults = $searchResultIterator.FetchAll()
        
        return $searchResults.Length -eq 0
    }
    finally
    {
        # Release resources
        if ($searchResultIterator){ $searchResultIterator.Dispose() }
    }
}

# Check if the full name is unique.
if (IsFullNameUnique "%fullname%")
{
    return
}

# If the full name is not unique, generate a unique one.
$uniqueFullname = $Null
for ($i = 1; $True; $i++)
{
    $uniqueFullname = "%fullname%" + $i
    if (IsFullNameUnique $uniqueFullname)
    {
        break
    }
}

# Rename the object.
$Context.SetModifiedPropertyValue("name", $uniqueFullname)
$Context.LogMessage("Full Name has been changed to $uniqueFullname", "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.