Import-MailContactsFromCSV.ps1 is a PowerShell script to automate the creation of Exchange mail contacts using a CSV file for input.

The current version of the script supports a CSV file containing the following fields, which are the minimum details required for creating a mail contact in Exchange:

  • Name
  • ExternalEmailAddress

The script looks for a file named Contacts.csv by default, or you can use the optional -CSVFileName parameter to specify your own file name. You can also set a default OU for contacts in the script, or use the -OU parameter to specify an OU.

Download the script from the TechNet Gallery or GitHub.

Example usage:

C:ScriptsImportContacts>.Import-MailContactsFromCSV.ps1

C:ScriptsImportContacts>.Import-MailContactsFromCSV.ps1 -CSVFileName externalcontractors.csv

C:ScriptsImportContacts>.Import-MailContactsFromCSV.ps1 -OU "exchangeserverpro.net/Company/Contacts"

If you are providing this script to a non-administrator to use, you can configure an RBAC custom role to limit their permissions to only allow them to create contacts in a specific OU, as demonstrated here.

About the Author

Paul Cunningham

Paul is a former Microsoft MVP for Office Apps and Services. He works as a consultant, writer, and trainer specializing in Office 365 and Exchange Server. Paul no longer writes for Practical365.com.

Comments

  1. Tarek

    how to add the mail contacts without OU?

  2. Tarek Halloun

    hello man , how to do it without OU?

      1. Tarek

        hello Tony , what to comment out exactly in the script ?!

  3. Miguel B.

    This was extremely helpful after a migration to Google, where contacts needed to be created for all users in the new google domain to enable forwarding.

    Thanks a lot!! Saved me possibly a few hours or coding / testing something similar.

  4. Alejandrina

    This blog was… how do you say it? Relevant!! Finally I’ve found something which helped me.
    Thank you!
    cheap essay editing service cheap essay editing service

  5. Pedro Maria

    The term ‘Import-MailContactsFromCSV.ps1’ is not recognized as the name of a cmdlet, function, script file, or operable

    1. Andrew Tozzi

      Make sure you dot source the script, if you’re in the correct directory, you still need to preface the filename with “./”. Powershell will do this for you if you use the tab completion feature by typing some of the filename and then the tab key, e.g. “Update-MailCo” .

  6. cheryl

    I tried code:

    Import-Csv .\ExternalContacts.csv|%{New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName}

    I get this error:

    The external e-mail address joseph.smith@verizonwireless.com is not an SMTP e-mail address.
    + CategoryInfo : NotSpecified: (NAMPR18A001.PRO….com/Joe Smith:ADObjectId) [New-MailContact], Recipien
    tTaskException
    + FullyQualifiedErrorId : [Server=DM5PR1801MB1995,RequestId=ffea9430-096d-4d70-a70b-5b08387a160b,TimeStamp=5/14/20
    19 6:32:22 PM] [FailureCategory=Cmdlet-RecipientTaskException] 5870EC2D,Microsoft.Exchange.Management.RecipientTas
    ks.NewMailContact
    + PSComputerName : outlook.office365.com

    what does it mean it is not an SMTP email address?
    very novice on powershell… just trying to upload our csv file of contacts into exchange admin users…
    thank you

    1. Matt Dodd

      Make sure there is no blank space after the email address preceding the 1st comma in each record.
      I had the same error that I didn’t see when opening the CSV file in excel but it was evident when using notepad. Once this space was removed it worked.

  7. Alessandro Tinivelli

    @Paul: you have forgotten to write exactly how the .csv should be written.
    I had to reverse engineer your code in order to understand that.

    the file should be like this

    Name,ExternalEmailAddress
    john,john@example.com

  8. CaZzzle

    I keep getting an OU related error.

    Command I use:
    Import-MailContactsFromCSV.ps1 -CSVF
    ileName C:\IT\importimport.csv -OU “domain.org/MyBusiness/Users/TargetOU”
    The OU domain.org/MyBusiness/Users/TargetOU was not found
    At C:\Users\me\Downloads\Import-MailContactsFromCSV-V1.00\Import-MailContactsFromCSV.ps1:64 char:5
    + throw $message
    + ~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (The OU …e was not found:String) [], RuntimeException
    + FullyQualifiedErrorId : The OU domain.org/MyBusiness/Users/TargetOU was not found

    1. Alessandro Tinivelli

      @CaZzzle you must run the script in the Exchange Powershell

Leave a Reply