Scenario: Say you have been using Public Folders in Exchange to share contacts among users or you have a master contacts database somewhere. If you wanted to create these contacts in AD/Exchange with the least amount of effort, what would you do? Ok…so this scenario doesn’t really come up that often. Hopefully you can apply the below method to other processes that might come up.
First things first: We need to get the contacts in a CSV file. I can’t really tell you how to do that, but most likely if you are in Outlook you can use the Import/Export utility built in. Once you have the CSV file, open that file into a text editor or Excel and format the column headers. Then save the file into a known location on the Exchange server (I saved mine in C:\Temp\contacts-imp.csv). Here is what my example CSV file looks like:
Now it is time to create the contacts with the information in the CSV file. Unfortunately, you cannot do this in one step due to the limitations of the New-MailContact cmdlet. I guess we will do it in three steps.
1) Open ESM, and first we import the CSV file into a variable that we can refer to later.
$csvinfo = import-csv "C:\temp\contacts-imp.csv"
2) Create the mail enabled contacts by reading the information from the variable.
ForEach ($line in $csvinfo) {New-MailContact -Name $line.DisplayName -FirstName $line.FirstName -LastName $line.LastName -ExternalEmailAddress $line.EmailAddress -DisplayName $line.DisplayName}
Opening up EMC shows the newly created mail enabled contacts.
3) Now that we have our contacts created and email enabled, it is time to fill in the gaps with all the other desired attributes. We will need to import the Active Directory module into ESM so that we can manipulate the contact objects. Finally, we read the CSV file and fill in the values for the other attributes.
Import-Module ActiveDirectory
ForEach ($line in $csvinfo) {Set-ADObject -identity "CN=$($line.DisplayName),CN=Users,DC=lab,DC=local" -add @{company = $line.Company;l = $line.BusinessCity;st = $line.BusinessState;title = $line.JobTitle;mobile = $line.MobilePhone;telephonenumber = $line.BusinessPhone}}
That is it! By opening the contact we see all the information that we imported and the contact is now in the GAL.
If you need to find out what other LDAP attributes you can manipulate, just run “CSVDE -f filename.csv” from a command prompt. Any of these attributes can be inserted into the CSV file and used in the PowerShell script. Happy importing.