CSV import to Global Address List

Elliott Clark
Elliott Clark used Ask the Experts™
on
Hi Experts,

I am trying to import user data from a CSV file into our GAL.

Here's my command
 Import-Csv "C:\Temp\Contacts.csv" | ForEach-Object { New-MailContact -Name $_.Name -ExternalEmailAddress $_.Email-Address -FirstName $_.FirstName -LastName $_.LastName -OrganizationalUnit "ad.co.uk\ou_H‌‌ierarchicalAddressBook" -Alias $_.Name }

Open in new window


I've also attached an example of the CSV I've been using.

below is the error I am getting, any help would be greatly appreciated

ErrorContact.csv
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Michael B. SmithManaging Consultant

Commented:
Get rid of the "-" in Email-Address. If a CSV header isn't just alphanumeric, it needs to be quoted. And that leads to all sorts of complications.

Also, in your demo Contact.csv you spelled Firstname incorrectly.
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
The problem is the Dash in "Email-Address" property.
To solve this you should use single '

like this:
 Import-Csv .\Contact.csv | ForEach-Object { 
    New-MailContact -Name $_.Name -ExternalEmailAddress $_.'Email-address' -FirstName $_.FirstName -LastName $_.LastName -OrganizationalUnit "ad.co.uk\ou_H‌‌ierarchicalAddressBook" -Alias $_.Name 
}

Open in new window

FOXActive Directory/Exchange Engineer
Top Expert 2015

Commented:
Elliot - if your header reads Email-address then using the quotes as Jose has stated will correct your issue.

Send a screenshot of the headers on your .csv file so we can see what your issue is.  Or just change your header to emailaddress and in your script let it read  -ExternalEmailAddress  $_.emailaddress
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
FOX the CSV file is attached on the question .
Michael B. SmithManaging Consultant

Commented:
We've all given the same answer in different ways. Let the poor OP respond. :-)
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
We should be glad he can pick one of all :) at least he has options haha

Author

Commented:
Hi All,

thank you for the overwhelming response!

Silly mistake by me I have taken the hyphen out however i'm getting a weirder error and I'm not sure if its the user data I have so here's another screenshot

I'm wondering if this is because there is a space in the full name?
If so this causes me quite a few issues as I have 10,000 records!

thanks for your help so far :)
Capture.PNG
FOXActive Directory/Exchange Engineer
Top Expert 2015

Commented:
Elliot,
Test the below on one contact- we can't have spaces in aliases so let's do FirstName_LastName to create the alias

Import-Csv "C:\Temp\Contacts.csv" | ForEach-Object { New-MailContact -Name $_.Name -ExternalEmailAddress $_.EmailAddress -FirstName $_.FirstName -LastName $_.LastName -OrganizationalUnit "ad.co.uk\ou_H‌‌ierarchicalAddressBook" -Alias  (($_.FirstName) + "_" + ($_.LastName))}

Author

Commented:
thanks guys!

last thing,  any idea how I can add attributes like department, job title and company?

thanks
Active Directory/Exchange Engineer
Top Expert 2015
Commented:
Elliot to add the department, job title and company add those fields to your .csv and then we would have to combine the other command with the first one or run it seperately.  The below is combined in one script.  Being that you said you have 10000 records we will pause it for about 8 minutes to give it time to create all the contacts before we set the other attributes

Import-Csv "C:\Temp\Contacts.csv" | ForEach-Object { New-MailContact -Name $_.Name -ExternalEmailAddress $_.EmailAddress -FirstName $_.FirstName -LastName $_.LastName -OrganizationalUnit "ad.co.uk\ou_H‌‌ierarchicalAddressBook" -Alias  (($_.FirstName) + "_" + ($_.LastName))}
start-sleep -s 480
import-csv "C:\Temp\Contacts.csv"  | foreach-Object {Set-contact -identity $_.emailaddress  -Company $_.Company -Department $_.Department  -Title $_.Title}

Author

Commented:
thank you to everyone who assisted me with this

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial