add_contacts.ps1

#
# add_contacts.ps1
# 
# Written by Carol Wapshere, October 2008
#
# Use to create one or more contacts.
# If the contact exists already the create command will throw an error, but the attributes will still be updated.
#
# Instructions:
#
#   1. Modify the add_contacts.csv file to include information about the contacts to add.
#      The file must have a header row and must include the following fields:
#            displayName,sn,givenName,name,mail,telephoneNumber,facsimileTelephoneNumber,mobile,homePhone,streetAddress,company
#
#   2. Open the Exchange Management Shell
#
#   3. CD to C:\scripts and run add_contacts.ps1.
cat add_contacts.csv > unicode.csv # make utf8
import-csv unicode.csv | foreach {
write-host
write-host
write-host "Creating contact for " $_.displayName
New-MailContact -ExternalEmailAddress $_.mail -DisplayName $_.displayName -Name $_.name -OrganizationalUnit 'bbhfgg.local/BBH/Contacts' -FirstName $_.givenName -LastName $_.sn
$ldapstring = "LDAP://CN=" + $_.name + ",OU=Contacts,OU=BBH,dc=bbhfgg,dc=local"
write-host $ldapstring
$contact = [ADSI]$ldapstring
write-host
write-host "Setting AD attributes..."
if ($_.displayName -ne "")
{
  write-host "    displayName: " $_.displayName
  $contact.Put("displayName", $_.displayName)
}
if ($_.sn -ne "")
{
  write-host "    sn: " $_.sn
  $contact.Put("sn", $_.sn)
}
if ($_.givenName -ne "")
{
  write-host "    givenName: " $_.givenName
  $contact.Put("givenName", $_.givenName)
}
if ($_.company -ne "")
{
  write-host "    company: " $_.company
  $contact.Put("company", $_.company)
}
if ($_.telephoneNumber -ne "")
{
  write-host "    telephoneNumber: " $_.telephoneNumber
  $contact.Put("telephoneNumber", $_.telephoneNumber)
}
if ($_.facsimileTelephoneNumber -ne "")
{
  write-host "    facsimileTelephoneNumber: " $_.facsimileTelephoneNumber
  $contact.Put("facsimileTelephoneNumber", $_.facsimileTelephoneNumber)
}
if ($_.mobile -ne "")
{
  write-host "    mobile: " $_.mobile
  $contact.Put("mobile", $_.mobile)
}
if ($_.homePhone -ne "")
{
  write-host "    homePhone: " $_.homePhone
  $contact.Put("homePhone", $_.homePhone)
}
if ($_.streetAddress -ne "")
{
  write-host "    streetAddress: " $_.streetAddress
  $contact.Put("streetAddress", $_.streetAddress)
}
$contact.SetInfo()
} | out-file -filepath "c:\scripts\add_contacts.log" -encoding "UNICODE"