Office 365

Office 365 – How to migrate contacts from Exchange

During an Office 365 migration from an Exchange on-premises, it can be usefull to migrate contacts information. For some reasons, if you can not do a Active Directory synchronization, contacts information needs to be imported manually.

You can easily do that by running two scripts, one in your Exchange environnement which creates an csv file, and the other into the Office 365 Powershell.

Migrate contacts

Export

The first thing to do is to export the actual contact list using this Powershell script:

$CSVFileName="Contacts_Export.csv"
 
#Create the CSV file
New-Item $CSVFileName -type file -force
 
#Write the first line into the CSV file
Add-Content $CSVFileName "DisplayName,Name,ExternalEmailAddress,FirstName,LastName,StreetAddress,City,StateorProvince,PostalCode,Phone,MobilePhone,Pager,HomePhone,Company,Title,OtherTelephone,Department,Fax,Initials,Notes,Office,Manager"
 
#Get Contact from Exchange
$Contacts= Get-Contact
 
#For each contact
foreach($Contact in $Contacts)
{
	$Name =$Contact.name	
	$FirstName =$Contact.FirstName
	$LastName=$Contact.LastName
	$StreetAddress=$Contact.StreetAddress
	$City =$Contact.City
	$StateorProvince =$Contact.StateorProvince
	$PostalCode =$Contact.PostalCode
	$Phone =$Contact.Phone
	$MobilePhone =$Contact.MobilePhone
	$Pager =$Contact.Pager
	$HomePhone =$Contact.HomePhone
	$Company =$Contact.Company
	$Title =$Contact.Title
	$OtherTelephone=$Contact.OtherTelephone 
	$Department=$Contact.Department
	$Fax =$Contact.fax
	$Initials=$Contact.initials 
	$Notes=$Contact.notes
	$Office=$Contact.office 
	$Manager=$Contact.manager
	$DisplayName=$Contact.DisplayName
 
	$MailContact=Get-MailContact -Identity $Name
	$ExternalEmailAddress =$MailContact.ExternalEmailAddress
 
	#Write information into the CSV file	
	Add-Content $CSVFileName "$DisplayName,$Name,$ExternalEmailAddress,$FirstName,$LastName,$StreetAddress,$City,$StateorProvince,$PostalCode,$Phone,$MobilePhone,$Pager,$HomePhone,$Company,$Title,$OtherTelephone,$Department,$Fax,$Initials,$Notes,$Office,$Manager"
}

Import

Once you got the CSV file, you can import it into your Office 365 tenant using this Powershell script:

param(
[parameter(Mandatory=$true)][String]$FileCSV
)
 
$Contacts = Import-Csv $FileCSV
 
Foreach ($Contact in $Contacts){
 
        $Name =$Contact.name	
	$FirstName =$Contact.FirstName
	$LastName=$Contact.LastName
	$StreetAddress=$Contact.StreetAddress
	$City =$Contact.City
	$StateorProvince =$Contact.StateorProvince
	$PostalCode =$Contact.PostalCode
	$Phone =$Contact.Phone
	$MobilePhone =$Contact.MobilePhone
	$Pager =$Contact.Pager
	$HomePhone =$Contact.HomePhone
	$Company =$Contact.Company
	$Title =$Contact.Title
	$OtherTelephone=$Contact.OtherTelephone 
	$Department=$Contact.Department
	$Fax =$Contact.fax
	$Initials=$Contact.initials 
	$Notes=$Contact.notes
	$Office=$Contact.office 
	$Manager=$Contact.manager
	$ExternalEmailAddress =$Contact.ExternalEmailAddress
	$DisplayName=$Contact.DisplayName
 
 
   New-MailContact -Name $Name -DisplayName $DisplayName -ExternalEmailAddress $ExternalEmailAddress -FirstName $FirstName -LastName $LastName
   Set-Contact $Name -StreetAddress $StreetAddress -City $City -StateorProvince $StateorProvince -PostalCode $PostalCode -Phone $Phone -MobilePhone $MobilePhone -Pager $Pager -HomePhone $HomePhone -Company $Company -Title $Title -Department $Department -Fax $Fax -Initials $Initials -Notes $Notes -Office $Office
 
}

More

You can get more information about the Get-Contact Powershell cmdlet here. And for Set-Contact Powershell cmdlet here.

Leave a Reply

Your email address will not be published. Required fields are marked *