question

BijuThankappan-2567 avatar image
0 Votes"
BijuThankappan-2567 asked IanXue-MSFT edited

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

Hi,

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

In the csv file contacts are listed like following:
bt@gmail.com
tb@gmail.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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered BijuThankappan-2567 commented

Hi,

Is bt@gmail.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.

· 15
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry...I was not clear earlier.

The Contacts present in the file have this format:
Contacts
anon@user
abc@gmail.com
Jim Ray
Mary Howlander

So, the file has 1 column with heading as Contacts and name and email both in the same column. Some are emails and some are contact names (first name last name) like above. And yes these are also the CN's as well.

The requirement is to ensure their CN is equal to their mail attribute value

TIA,
Biju

0 Votes 0 ·

Please see if this works. The contacts anon@user and abc@gmail.com will be renamed to xyz@contoso.com and abc@contoso.com.

 $file = "C:\temp\contacts.csv"
 Import-Csv -Path $file | Where-Object {$_.contacts -match '@gmail.com'} | ForEach-Object {
     $CN = $_.Contacts 
     Get-ADObject -LDAPFilter "(&(objectClass=contact)(CN=*$CN*))" | Rename-ADObject -NewName $CN.replace("gmail","contoso")
 }
0 Votes 0 ·

Thanks! But the file has 1 column and the rows are populated with names and emails. Both these names and emails are also CN's. Like below:

Contacts
anon@user
abc@gmail.com
Jim Ray
Mary Howlander

How do I ensure they all are considered and their CN's changed to their email.

TIA,
Biju

0 Votes 0 ·

What about Jim Ray and Mary Howlander? These are also CN's btw and present in the file.
Objective is to query these contacts, some with there display name and some with their emails from the csv file and then change their CN's to the value present in their email attribute.

0 Votes 0 ·
Show more comments
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

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.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Getting the following errors:

Get-ADObject : One or more properties are invalid.
Parameter name: targetAddress
At Z:\Scripts\changeall.ps1:5 char:10
+ Get-ADObject -Filter "objectClass -eq 'contact' -and CN -eq ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADObject], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADObject

0 Votes 0 ·

Yes rename the contact by changing the attibute 'name' for all contacts(CN can be fname.lname Or email) in csv. Then, change 'mail' and 'targetaddress' attributes
FYI...The above error might be because I do not have exchange in my lab.

0 Votes 0 ·

Thanks, Ian. Allow me to come back on this after testing in exchange environment

0 Votes 0 ·

Thanks a lot, Ian

Here is a small addon request:

The proxyaddresses AD attribute has multiple entries in it. I need to iterate each entry inside proxyaddresses and change @gmail.com to @contoso.com

TIA,
Biju

0 Votes 0 ·

Please 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,proxyAddresses | ForEach-Object{
          if($_.targetAddress -match 'gmail.com') {
              $NewAddress = $_.targetAddress.Replace('SMTP:','').Replace('gmail','contoso')
          }
          else{
              $NewAddress = $_.targetAddress.Replace('SMTP:','')
          }
          $NewName = "$($_.givenName).$($_.sn)@contoso.com"
          if($_.proxyAddresses){
             $NewProxyAddresses = ($_.proxyAddresses).Replace('gmail','contoso')
          }
          Set-ADObject -Identity $_ -Replace @{mail=$NewAddress;targetAddress='SMTP:'+$NewAddress;displayName=$NewName;proxyAddresses=$NewProxyAddresses} 
          Rename-ADObject -Identity $_ -NewName $NewName
      }     
  }
0 Votes 0 ·