Thursday 29 July 2010

Create new mail contacts from a CSV file for Exchange 2007/2010

Today I had a colleague come to me, asking for help as she had a user asking to have 649 mail contacts created and added to a distribution group by the end of the day.

Now instead of sitting there and slowly adding in each and every single contact individually (which if you've had to do this before in Exchange you'll know it's an absolute nightmare!!) I decided to modify a script I wrote a while back for importing mailboxes from a CSV file.

So the format of the CSV file should be:

NameEmailOUPAth
Test Usertest.user@domain.comdomain.local/Mail Contacts/Test

For the OUPath you can easily find this out by going into your Active Directory and right clicking on the folder you wish the mail contacts to be imported into, and going to Properties
OU Properties

As you can see in the Object section you can find out the OU Path, by looking at the Canonical name of object:

Once the .CSV file is created and formatted correctly you can use this wonderful little powershell script I wrote:


Import-CSV C:\createnewcontact.csv | ForEach-Object{
New-MailContact -ExternalEmailAddress $_.Email -Name $_.Name -OrganizationalUnit $_.OUpath
}


Now I'll talk you through section by section on what this script does so you're a little more aware (and i can prove I didn't just copy and paste from a website :p)

Now "Import-CSV C:\createnewcontact.csv" basically tells Powershell to import data from the CSV file which lives directly on the C:\ and is called createnewcontact.csv (bare in mind if you call your CSV differently you will need to change this section)

"ForEach-Object"{ allows you to perform an action on each item in the collection (in this case everything between the { }

"New-MailContact" This is kind of self-explainatory to be honest. If you want a new mailbox you put in New-Mailbox and for a new mail contact you use New-MailContact


Now when you run New-MailContact manually into PowerShell it asks you for 2 pieces of information:

  • ExternalEmailAddress

  • Name

So when your PowerShell script runs New-MailContact it still needs this information. So by using "-ExternalEmailAddress$_.Email" you are telling the script that when it gets asks what the -ExternalEmailAddress is the PowerShell sees $_.Email which signifies to it that it should get this information from the Email column ($_. tells it to use the column and the name after it is the column name)

This is the same for -Name $_.Name -OrganizationalUnit $_.OUpath

I'm not the greatest at explaining things so I do apologise. If in doubt just copy exactly what I've done and you'll be fine :)

Mailcontact Powershell

Pop in the location of the script into Power Shell (or if you're really lazy just drag and drop the script from Explorer into the Power Shell window) and hit enter and watch it run away and create hundreds of mail contacts from your .CSV file in seconds...and then have a beer

2 comments:

  1. t's such a great site. fabulous, very intriguing!!!-------

    ReplyDelete
  2. Awesome site, I hadn't come across tekctrl.blog.com before during my searches!Carry on the great work!

    ReplyDelete