Powershell script to change the CN of multiple contacts in AD from a CSV

Biju Thankappan 86 Reputation points
2021-06-08T06:12:05.11+00:00

Hi,

I'm trying to change the CN of multiple AD contacts in a csv from bt@Stuff .com to bt@Company portal .com

In the csv file contacts are listed like following:
bt@Stuff .com
tb@Stuff .com
...

Following is a working code for Users from the net. How do we do it for contacts?

Import-Module activedirectory$varCSV = "users.csv" $userlist = Import-Csv -Path $varCSV -Delimiter ","foreach ($user in $userlist){ $SamAccountName = $user.SamAccountName $FirstName = $user.GivenName $LastName = $user.Surname $DisplayName = $user.GivenName + " " + $user.Surname $UserPrincipalName = $user.UserPrincipalName + "@students.stdeclanscollege.ie" $JobTitle = $user.JobTitle $EmailAddress = $user.UserPrincipalName $Department = $user.Department $dn = (Get-ADUser -Identity $SamAccountName).DistinguishedName Get-ADUser -Identity $user.SamAccountName | Set-ADUser -DisplayName $DisplayName -GivenName $FirstName -Surname $LastName -Title $JobTitle -UserPrincipalName $UserPrincipalName -EmailAddress $UserPrincipalName -Department $Department Try { Rename-ADObject $dn -NewName $DisplayName } catch { Write-Output "Error Check Acc: " ($user.samaccountname) | Out-File C:\errors.txt -Append }}

TIA,
BT

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-06-08T08:28:33.777+00:00

    Hi,

    Is bt@Stuff .com the UPN or CN? If it's the CN, and assuming the header of the column is CN, you can rename the contacts like below

    $file = "C:\temp\contacts.csv"  
    Import-Csv -Path $file | ForEach-Object {  
        $CN = $_.CN  
        Get-ADObject -LDAPFilter "(&(objectClass=contact)(CN=*$CN*))" -Properties CN | Rename-ADObject -NewName $_.CN.replace("gmail","contoso")  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-06-23T02:21:57.063+00:00

    Hi,

    If you want to set the DisplayName property when you refer to the Full Name, you can try this

    $file = 'C:\temp\contacts.csv'  
    $ou = 'OU=test,DC=contoso,DC=com'  
    Import-Csv -Path $file | ForEach-Object {  
        $CN = $_.Contacts   
        Get-ADObject -Filter "objectClass -eq 'contact' -and CN -eq '$cn'" -SearchBase $ou -Properties targetAddress,sn,givenName | ForEach-Object{  
            if($_.targetAddress -match 'gmail.com') {  
                $NewAddress = $_.targetAddress.Replace('SMTP:','').Replace('gmail','contoso')  
            }  
            else{  
                $NewAddress = $_.targetAddress.Replace('SMTP:','')  
            }  
            $NewName = "$($_.givenName).$($_.sn)@contoso.com"  
            Set-ADObject -Identity $_ -Replace @{mail=$NewAddress;targetAddress='SMTP:'+$NewAddress;displayName=$NewName}   
            Rename-ADObject -Identity $_ -NewName $NewName  
        }       
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.