Create bulk email contacts in Exchange 2010

I'm trying to run a script that would allow me to create a multiple e-mail contacts from the outside vendors and getting all sorts of errors.
Here is an example of my .csv file

Name, First, Last, External E-mail Address
Adam Scott, Adam,Scott,

This is a script that i tried to run:

Import-Csv C:\temp\contacts12.csv | ForEach { New-MailContact -Name $_.displayName -Firstname $_.FirstName -LastName $_.LastName -ExternalEmailAddress $_.ExternalEmailAddress -OrganizationalUnit "" }

I've seen some recommendations on this site, followed it and still doesn't work.

Thanks in advance,

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joe PriorCommented:
Hii swolff1..

Are you getting any error message ?
swolff1ITAuthor Commented:
I'm getting this message:

Cannot bind argument to parameter 'ExternalEmailAddress' because it is null.
    + CategoryInfo          : InvalidData: (:) [New-MailContact], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,New-MailContact
Will SzymkowskiSenior Solution ArchitectCommented:
This is the one that i use to create new mail contacts.

Make sure that your column headings in the CSV file are like below...
Name            First             Last            ExternalEmail
Adam           Scott             Adam         Scott

Also in my script below i have defined an $OU variable and $DC variable. These are where the contacts will be stored rather than the default Users container. I also like to point to a single DC in the event the command communicates to another one during the script. If you do not want to use those variables just delete them.

$Users = Import-Csv "c:\filename.csv"
$OU = "External Contacts"
$DC = ""
       ForEach ($EM in $Users) {

            New-MailContact -Name $EM.Name -FirstName $EM.First -LastName $EM.Last -ExternalEmailAddress $EM.ExternalEmail -OrganizationalUnit $OU -DomainController $DC


Open in new window

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Murali ReddyExchange ExpertCommented:
There is a space in the csv columns.. "External E-mail Address" It should be ExternalEmailAddress  as per your script reeading. Otherwise it is all good.
swolff1ITAuthor Commented:
OK. I'm using this exact script:

 Import-CSV C:\Temp\contacts12.csv | Foreach { New-MailContact -Name $EM.Name -FirstName $EM.First -LastName $EM.Last -ExternalEmailAddress $EM.ExternalEmail -OrganizationalUnit $TEST -DomainController $ }

here is my .cvs file

Adam Scott,Adam,Scott,adams@acc
Adil Quyser,Adil,Quyser,adilq@acc

this is an error I'm getting:

Cannot bind argument to parameter 'Name' because it is an empty string.
    + CategoryInfo          : InvalidData: (:) [New-MailContact], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,New-MailContact

what am I missing?

Murali ReddyExchange ExpertCommented:
You are mixing both so getting to errors, do one thing.. Do exactly below, copy paste..

$temp = Import-CSV C:\Temp\contacts12.csv
Foreach ($EM in $temp)
{ New-MailContact -Name $EM.Name -FirstName $EM.First -LastName $EM.Last -ExternalEmailAddress $EM.ExternalE-mail -OrganizationalUnit ""}

Makesure you edit the Organizational Unit with either DN or CanonicalName value of OU.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Will SzymkowskiSenior Solution ArchitectCommented:
Yes, you also have the wrong column name for ExternalEmail.

You have External-Email which will not work.

Joe PriorCommented:
The $_.ExternalEmailAddress in the command doesn't match the CSV column header you listed. Change the header to ExternalEmailAddress.

Hope it Will work

swolff1ITAuthor Commented:
Thank you for your replies,

When I run the script, I don't see any error messages but I don't see any created names in the AD and not in the Exchange "Mail Contacts", below the exact script and .csv  file:

VERBOSE: Connected to
[PS] C:\Windows\system32>$temp = Import-CSV C:\Temp\contacts12.csv
[PS] C:\Windows\system32>Foreach ($EM in $temp)
>> { New-MailContact -Name $EM.Name -FirstName $EM.First -LastName $EM.Last -ExternalEmailAddress $EM.ExternalE-mail -Or
ganizationalUnit $TEST }

.csv file:

Adam Scott,Adam,Scott,adams@acc
Adil Quyser,Adil,Quyser,adilq@acc

Thanks again.
Will SzymkowskiSenior Solution ArchitectCommented:
You have a - DASH in the external email address column. REMOVE the -DASH.

swolff1ITAuthor Commented:
Here is what I get...

[PS] C:\Windows\system32>Import-CSV C:\temp\contacts12.csv | Foreach ($EM in $temp) { New-MailContact -Name $EM.Name -Fi
rstName $EM.First -LastName $EM.Last -ExternalEmailAddress $EM.ExternalEmail  -OrganizaninalUnit $TEST }
Unexpected token 'in' in expression or statement.
At line:1 char:52
+ Import-CSV C:\temp\contacts12.csv | Foreach ($EM in <<<<  $temp) { New-MailContact -Name $EM.Name -FirstName $EM.Firs
t -LastName $EM.Last -ExternalEmailAddress $EM.ExternalEmail  -OrganizaninalUnit $TEST }
    + CategoryInfo          : ParserError: (in:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

my .csv file

Adam Scott,Adam,Scott,adams@acc
Adil Quyser,Adil,Quyser,adilq@acc

Thanks in advance.
Will SzymkowskiSenior Solution ArchitectCommented:
You are not even using my script. Use Exactly what i have typed out in my first post and it will work. I have tested this already and it works in my lab 100%.

As you can see by the error below you are not even putting the import-csv into a variable. USE MY SCRIPT AND IT WILL WORK!
Import-CSV C:\temp\contacts12.csv | Foreach ($EM in <<<<  $temp)

Sorry for the caps, just hate repeating myself.

swolff1ITAuthor Commented:

I apologize for the last reply. Murali, your script works, Joe and Will Thank you for pointing out the dash. Feel stupid, I'm not into scripts.

Thank you again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.