My old process was to use CSVDE to import the CSV file into the ActiveDirectory OU "Students". However, this process required that I delete all 1,800 or so contacts and then reimport. This was not very elegant. So, after a lot of fruitless searching, I decided to write this script to handle our Student OU that contains only contact objects for the Exchange GAL.
This script first checks the existing contacts, based on email address, against the CSV file. If they exist in the file, great, do nothing, otherwise, delete the contact in the OU. Next, if the file has new contacts to import, import them! The script has two variables up in the "customizations" section that you can change for your specific OU and of course, the path to your CSV file's location. The structure of the CSV file is:
displayName,mail,givenName,sn,mailNickname,Company
The PowerShell (.ps1) script is below for anyone who could benefit from this. Make sure you run it as a user that has access to the OU you're syncing and enjoy!
#
#Exchange 2008 Contact sync with CSV file
#
#Set customizations here
$syncOU="Students"
$importFile="C:\temp\ExchangeImport.csv"
#
#Global Variables
$removeCounter=0
$addCounter=0
#
#Check existing contacts in OU against CSV file, delete if not found
write-host "Checking existing contacts for deletions..."
$existingContacts = get-recipient -resultSize unlimited -organizationalUnit $syncOU -recipientType MailContact |
select -expand EmailAddresses | %{$_.smtpAddress}
$existingContacts | forEach-object -process {
if ($_ -ne $NULL) {
$i = $_
if (import-csv $importFile | where-object {$_.mail -eq $i})
{}
else
{
write-host "Removing contact: " $i
Remove-MailContact -Identity $i -Confirm:$false
$removeCounter++
}
}
}
#
#Check CSV file for new addresses against OU, create if not found
write-host "Checking CSV file for new contacts..."
import-csv $importFile |
forEach-object -process {
if (get-contact -erroraction silentlycontinue -organizationalUnit $syncOU $_.mail)
{}
else
{
write-host "Adding contact from CSV file:"
new-mailContact -Name $.displayName -FirstName $.GivenName -LastName $_.sn `
-Alias $.mailNickname -ExternalEmailAddress $.mail `
-PrimarySmtpAddress $_.mail -OrganizationalUnit $syncOU
set-contact -identity $.mailNickname -WindowsEmailAddress $.mail -Company "$syncOU"
$addCounter++
}
}
#
#Statistics Output
write-host "Attempted to delete " $removeCounter " contacts from the " $syncOU " OU."
write-host "Attempted to add " $addCounter " contacts to the " $syncOU " OU."

