Import Contacts CSV Into Exchange 2010

Problem:  I need to import 700 of our “key client contacts” into Exchange 2010 from a CSV file

Solution:  Powershell!

Step 1:  Create the CSV

There are a ton of different fields you can use in your CSV to import into Exchange contacts, I am not going to go into them all here, but just check out the set-contact Powershell cmdlet for all your choices.

Because this was a fairly simple import, this is what the headers of my CSV file looked like.

Step 2:  Create the Powershell Script

 Import-CSV C:contacts.csv | ForEach {
 New-MailContact -Name $_.ContactFullName -ExternalEmailAddress $_.ContactEmail
-FirstName $_.ContactFName -LastName $_.ContactLName
-OrganizationalUnit “domain.com/Folder Structure Here”

 Set-Contact -Identity $_.ContactFullName -Phone $_.”ContactPhone” -MobilePhone $_.”ContactMobile”

 }

The first part creates a new mail contact using the mappings in the script.  So, the Name field in the Exchange contact uses the ContactFullName field in the CSV, the ExternalEmailAddress in the Exchange contact uses the ContactEmail field in the CSV, etc.

The second part sets other attributes on the contact, in this example the work and cell phone numbers of each contact by using the -Identity parameter mapped to the same ContactFullName field in the CSV used first in the script.

Step 3:  Do Something Really Dumb and Make Life Difficult For Yourself

So let’s just imagine for a minute that you are cleaning up your CSV file and notice some of your phone numbers have periods in them, and that because you are who you are, that bothers you, so you do a simple find and replace and problem solved!  You import your contacts and everything is perfect and takes about 3 minutes.  Then you notice a problem.  All of your email address look like this:  exchangecontact@exchangecom.  Oh yeah, because you took all the periods out dummy!  So, after I pulled the CSV information out of our SQL database again, I was ready to do another import, but needed to clean up the existing contacts first.  In retrospect, I think I could have just used the set-contact cmdlet to do this an easier way, but I’m new to Powershell and this is the first thing that came to mind.  And what was that?  DELETE ALL OF THE CONTACTS I JUST IMPORTED!

 Import-CSV C:contacts2.csv | ForEach {
 Remove-MailContact -Identity $_.ContactFullName -Confirm:$false
 }

That little script used an identical CSV that I had created to delete all of the contacts by using their ContactFullName as their Exchange identity.  Then just re-run your import script and you are good to go.

I could have used a script like this to just update the existing contacts:

Import-CSV C:contacts3.csv | ForEach {
 Set-Contact -Identity $_.ContactFullName -ExternalEmailAddress $_.ContactEmail
 }

Leave a Reply