Friday, September 3, 2010

Bulk Contact Creation in Exchange With PowerShell and CSV Files

I recently ran into a situation where someone wanted to have Distribution Lists in Exchange that were populated with email addresses of people outside our company. Of course, this meant that each recipient needed its own Contact object in Active Directory. But how to create the dozens of AD objects I'd need to fulfill this simple request?

Luckily, there's a cmdlet for that. Create a CSV file where the first line reads as follows:

Name,ExternalEmailAddress,OrganizationalUnit



Subsequent lines should look like this:

John Doe,SMTP:johnny@example.com,contoso.info/In/This/OU
Jane Doe,SMTP:janey@example.com,contoso.info/In/That/OU



The finished product should look like this:

Name,ExternalEmailAddress,OrganizationalUnit
John Doe,SMTP:johnny@example.com,contoso.info/In/This/OU
Jane Doe,SMTP:janey@example.com,contoso.info/In/That/OU



(Of course, add as many entries as you like)

Got that in a CSV file? Good. Save it somewhere and fire up PowerShell on your Exchange server. Then, run the following:

import-csv Book1.csv | foreach {New-MailContact -Name $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -OrganizationalUnit $_.OrganizationalUnit -whatif}



Edit the file name as needed. I threw in the "-whatif" part so that you can see exactly what the command will do before actually doing it. As long as you like the results, you can take out the "-whatif" to go ahead and create the objects.