Tuesday, July 10, 2007

Bulk importing contacts from CSV to Exchange 2007

When doing a bulk import of contacts you can use the following Powershell command to read a CSV file.
Import-Csv contacts.csv | ForEach { New-MailContact -Name $_.displayName -Firstname $_.FirstName -LastName $_.LastName -ExternalEmailAddress $_.EmailNddress -OrganizationalUnit "YourDomain.Local/Contacts" }

For an excellent list of what else you can import with the New-MailContact command (everything after the "|" character) you can go to the linked web site.

As something I found out while trying to use this command you must have the same capitalization in _.Variable as you do for your column headers in the CSV file. Also you cannot have the CSV file open while you are doing your import.

12 comments:

  1. Just what I was looking for :) Thanks!

    ReplyDelete
  2. You can get a list of current contacts by running-
    Get-Contact | Export-Csv c:\temp\contacts.csv

    Then use this as your template for importing.

    ReplyDelete
  3. thank you! works like a charm.

    ReplyDelete
  4. Guys, hope you can help

    im trying to creat mail contacts from a csv file im using this PS command:

    Import-Csv contacts.csv | ForEach { New-MailContact -Name $_.displayName -Firstname $_.FirstName -LastName $_.LastName -ExternalEmailAddress $_.EmailNddress -OrganizationalUnit "YourDomain.Local/Contacts" }

    with this csv titles:


    Name,Firstname,LastName,ExternalEmailAddress,OrganizationalUnit

    I do not get an error the command prompt just goes to >>

    Thanks in advance

    ReplyDelete
  5. @Anonymous, I don't run/have access to Exchange any more, so unfortunatly I can't help you with the problem, but you may have some luck over on ServerFault.

    ReplyDelete
  6. Thank you, was nice to get contacts from a 2007 exchange org to a 2010 server...

    ReplyDelete
  7. Thanks SO much! This was exactly what I needed and it took a long time to find it! I used this for taking my client info from Nav, to Excel(csv), named the fields accordingly and imported this way.

    ReplyDelete
  8. Hi there,
    Although I've seen people happy reading this solution, for me it's still a pain.
    I have a CSV file, and also the columnnames are exactly as in the artikel. However, I get the same error (see below) over and over again, and it's getting very frustrating.
    Maybe one of you guys can release this frustration and tell me what the h* I'm doing wrong ...
    Thanks in advance!
    Filip Van Put


    The error message ...:


    New-MailContact : Cannot bind argument to parameter 'Name' because it is null.
    At C:\TEMP\Bulk_Import_Contacts.ps1:3 char:47
    + ForEach ($line in $csv) {New-MailContact -Name <<<< $line.DisplayName -FirstName $line.FirstName -LastName $line.Las
    tName -ExternalEmailAddress $line.EmailAddress -DisplayName $line.DisplayName -OrganizationalUnit "nova.local/JUNKERS/C
    ontacts/Customers/GRO_AKP"}
    + CategoryInfo : InvalidData: (:) [New-MailContact], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Exchange.Management.RecipientTa
    sks.NewMailContact

    ReplyDelete
  9. I was able to get this to work with some tweaks.

    First I had to add a column into my csv for "DisplayName" and anotehr for "Name" and make sure they were both populated.

    Next I found that I had to convert the csv file to UTF8 encoding. Run this command from powershell: cat filename.csv > filename2.csv # make utf8

    Then I was able to import using this command:
    Import-CSV filename.csv | ForEach { New-MailContact –Displayname $_.DisplayName –Firstname $_.”First Name” –LastName $_.”Last Name” –ExternalEmailAddress $_.”E-mail Address” –Name $_.Name –OrganizationalUnit “domain.com/Contacts” }

    The items in quotes MUST be spelled the exact same as the headers in your CSV file.

    I hope that helps someone!

    -Tom

    ReplyDelete
  10. I was able to get this to work with some tweaks.

    First I had to add a column into my csv for "DisplayName" and anotehr for "Name" and make sure they were both populated.

    Next I found that I had to convert the csv file to UTF8 encoding. Run this command from powershell: cat filename.csv > filename2.csv # make utf8

    Then I was able to import using this command:
    Import-CSV filename.csv | ForEach { New-MailContact –Displayname $_.DisplayName –Firstname $_.”First Name” –LastName $_.”Last Name” –ExternalEmailAddress $_.”E-mail Address” –Name $_.Name –OrganizationalUnit “domain.com/Contacts” }

    The items in quotes MUST be spelled the exact same as the headers in your CSV file.

    I hope that helps someone!

    -Tom

    ReplyDelete
  11. make sure there are no blank columns in between

    ReplyDelete
  12. And also make sure you are using comma and not semi colon as a separator in the csv file...

    ReplyDelete