locked
Powershell Duplicate Display Name RRS feed

  • Question

  • I'm in the process of working on a script that will move users between OU's, however due to the large number of users there are a few that have duplicate display names.  Is there a way to have powershell check for a duplicate display name and if one is found rename the duplicate display name to include a number?
    Tuesday, November 14, 2017 6:22 PM

All replies

  • #here you have all the users DisplayName in the whole infrastructure
    
    $GetAllUsersDisplayName = (get-aduser -Filter * -properties DisplayName).DisplayName
    #Now you need to go comparing one by one
    
    $AllDisplayNames = $GetAllUsersDisplayName
    $duplicates=@()
    
    function GetDuplicatedByDisplayName{
        [Cmdletbinding()]
        param(
            [Parameter(Mandatory=$true,position=0)]$DN #DisplayName
        )
        BEGIN{
        $USERS = get-aduser -Properties DisplayName -filter { DisplayName -eq "$DN"}
        [int]$i=0;
        }
        PROCESS{
            if($USERS.count -gt 1){
                $i++ #increase the number in 1
                Write-Host "Working with Displayname Duplicated - $DN"
                foreach($user in $USERS){
                    $NewDN = $DN + "$i"
                    $DistinguishedName = $User.DistinguishedName
                    set-aduser $DistinguishedName -DisplayName "$NewDN"
                }
            }
        }
        END{
            
        }
    }
    
    foreach($user in $GetAllUsersDisplayName){
        #for this user check if there's any duplicate
        foreach($displayname in $AllDisplayNames){
            if($displayname -eq $user){
                $duplicates+=$user
            }
        }
    }
    
    #Now all the duplicates are within the duplicated 
    
    foreach($duplicate in $duplicates){
        GetDuplicatedByDisplayName -DN $duplicate
    } 

    Tuesday, November 14, 2017 7:27 PM
  • I believe instead of the displayName attribute, The_Chuckster is referring to the cn attribute, which must be unique in the OU or container. There are no restrictions (other than length) to displayName.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, November 14, 2017 9:29 PM
  • My standard approach is measure the effort before start to write code and I need to know exactly all the requirements before design, especially if is a process done once and so simple that is very difficult to generate errors.

    In this scenario, first of all I would like to know how many duplicates displayname are in AD.  So my approach will be to extract from AD displayname and samaccountname (my unique identifier) for all users :

     get-aduser -filter * -properties Displayname | select displayname, samaccountname | Export-Csv myaduser.csv

    and use EXCEL to find duplicate results (conditional formatting, highlight cells rules, duplicate values).

    There is a chance if the number of duplicates is too small to justify a script and this will be a process to run once, as said before,  with no need of any script.


    • Edited by Paolo.Frigo Tuesday, November 14, 2017 11:55 PM typos
    Tuesday, November 14, 2017 10:27 PM
  • Duplicate display names:

    Get-AdUser -filter * -properties Displayname | Group DisplayName | Where{$_.Count -gt 1} | select Name


    \_(ツ)_/


    • Edited by jrv Tuesday, November 14, 2017 10:30 PM
    Tuesday, November 14, 2017 10:30 PM
  • Thanks JRV, I didn't thought to use group-object in my example but it makes the script a really simple and readable oneliner. 
    • Edited by Paolo.Frigo Tuesday, November 14, 2017 11:50 PM
    Tuesday, November 14, 2017 11:50 PM
  • And ... you cab use the "group' to rename the accounts before trying to move them.


    \_(ツ)_/

    Wednesday, November 15, 2017 12:09 AM