Create contacts using powershell

oday 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:

Name Email OUPAth
Test User test.user@domain.com domain.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

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-explanatory be be honest. If you want a new mailbox you put 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 -ExternalEmailAddressis the PowerShell sees $_.Email which signifies to it that it should get this information from the Email column ($_. tells it to use a 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

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

Advertisements

2 comments

  1. Ben · November 14, 2013

    Great 🙂 how could I include telephone umber as well?

  2. best costa rica real estate · May 19, 2014

    Hi! This post could not be written any better!
    Reading this post reminds me of my previous room mate! He always kept chatting about this.
    I will forward this article to him. Fairly certain he will have
    a good read. Many thanks for sharing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s