Bulk Create Active Directory Contact Objects

By | December 13, 2009

If you’re creating contact objects in Active Directory the Exchange cmdlets New-MailContact, Set-MailContact and Set-Contact are usually sufficient.  On the other hand I haven’t found a way using these cmdlets to set all the attributes that I might need.  For example, the “description” attribute doesn’t appear to feature anywhere.

Things have obviously changed with the AD Powershell Provider and associated cmdlets in Windows Server 2008 R2, but here’s a script to bulk create contacts  from CSV file if you’re still using Powershell 1.0.

The format of the requried CSV file looks like this:

givenName,sn,displayName,mail,description
Bob,Smith,”Bob Smith”,bob.smith@gmail.com,”External Supplier”
Sue,Jones,”Sue Jones”,sue.jones@hotmail.com,”Hadware Sales”
Graeme,Turner,”Graeme Turner”,graeme.turner@yahoo.com,partner

#########################################################
#
# Name: BulkCreateContacts.ps1
# Author: Tony Murray
# Version: 1.0
# Date: 13/12/2009
# Comment: PowerShell 1.0 script to
# bulk create AD Contact objects from csv file
#
#########################################################

# Set the target OU where the contacts will be created
$ContactOU=[ADSI]“LDAP://ou=Contacts,dc=mycompany,dc=com“

# Find our current working directory
$working = $(Get-Location)

# Specify the folder and CSV file to use
$folder = “C:\util\Powershell\CSV”
Set-Location $folder
$csv = Import-Csv “contacts.csv”

# Parse the CSV file line by line
foreach($line in $csv) {

# Assign variables to each attribute
$givenName = $line.givenName
$sn = $line.sn
$displayName = $line.displayName
$mail = $line.mail
$description = $line.description
$targetAddress = $line.mail

# Go ahead and create the contact object
$Contact = $ContactOU.create(“Contact”,”cn=$displayName”)
# Set the attributes on the contact object
$Contact.Put(“givenName”,$givenName)
$Contact.put(“sn”,$sn)
$Contact.put(“displayName”,$displayName)
$Contact.put(“mail”,$mail)
$Contact.put(“description”,$description)
$Contact.put(“targetAddress”,$targetAddress)
# Commit the changes
$Contact.setinfo()
# Mail-enable the contact (if you need to)
Enable-MailContact -Identity $displayName -ExternalEmailAddress $targetAddress
}
# Go back to the original working directory
Set-Location $working

5 thoughts on “Bulk Create Active Directory Contact Objects

  1. Whammer

    question, i need to also make sure the contact is created in a specific path. How can i adjust your script to do so?

    Reply
  2. Whammer

    So you delete my previous comment. RTFM!!!

    So the Enable-MailContact doesn’t work for me. Any ideas?

    Reply
  3. Adam

    Just a quick note on this. If the CSV you use has “Null” (empty) values for any of the attributes, the setinfo() function will error out saying that the syntax was incorrect. This can be resolved by using an IF statement to compare the input string to $NULL, with the attribute assignment as the command for the IF statement, like this:

    if ($givenname -notlike $null){$Contact.Put(“givenName”,$givenName)}
    if ($sn -notlike $null){$Contact.put(“sn”,$sn)}
    if ($displayname -notlike $null){$Contact.put(“displayName”,$displayName)}

    etc.

    Doing this will basically tell the system that it should only assign the attribute if there is anything in it.

    Reply
  4. Adam

    Just a quick note on this. If the CSV you use has “Null” (empty) values for any of the attributes, the setinfo() function will error out saying that the syntax was incorrect. This can be resolved by using an IF statement to compare the input string to $NULL, with the attribute assignment as the command for the IF statement, like this:

    if ($givenname -notlike $null){$Contact.Put(“givenName”,$givenName)}
    if ($sn -notlike $null){$Contact.put(“sn”,$sn)}
    if ($displayname -notlike $null){$Contact.put(“displayName”,$displayName)}

    etc.

    Doing this will basically tell the system that it should only assign the attribute if there is anything in it.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.