none
Check AD for users with similar UPN RRS feed

  • Question

  • Hi All,

    I have an issue with the below script for the variable $existingNames. I am unable to get the other users with the same UPN (jesty.daniel2@domain.com,jesty.daniel3....)

    How can i trim the upn and get the list of users and get the proposed name. please assist.

    $User = Get-ADUser -Filter 'UserPrincipalName -like "jesty.daniel@domain.com"'
    $existingNames = $User.UserPrincipalName -join ','
    # Get the preferred/desired name and the existing variants in use
    $desiredName = "jesty.daniel@domain.com"
    $FirstName = 'Jesty'
    $DomainSFX = '@domain.com'
    $desiredUPN = $desiredName.Replace($DomainSFX,"")
    #$existingUPN = $existingNames.Trim($DomainSFX)
    # Parse the existing names and extract a list of indexes in use, e.g. 1,2,3
    $existingIndexList = $existingNames -split "," | ForEach-Object {$_.Trim($DomainSFX)} | ForEach-Object {$_ -replace $desiredUPN,""} | Where-Object {$_ -match "^\d+$"} | ForEach-Object {[int]$_}
    # Determine the highest index in use by sorting
    if (!($existingIndexList)){
        $lastIndex = 1
        }
    else {
        $lastIndex = $existingIndexList | Sort-Object | Select-Object -Last 1
    }
    # Increment to get next index and use this to create a proposed unique username
    $availableIndex = $lastIndex + 1
    $FirstName = $FirstName +' '+ $availableIndex
    $proposedName = $desiredUPN + $availableIndex


    Justin

    • Moved by Bill_Stewart Wednesday, May 30, 2018 6:36 PM Abandoned
    Thursday, April 26, 2018 5:02 PM

All replies