Import CSV file into Exchange Mail Contacts using powershell without duplicates

18,876

The article Importing exchange Mail Contacts using powershell suggests this code :

Import-Csv .\ExternalContacts.csv|
    foreach-object{if (Get-MailContact -anr $_.name) {write-host $_.name 'is a duplicate entry!!!'}     
    else {New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName}
    }
Share:
18,876

Related videos on Youtube

Mike Kellogg
Author by

Mike Kellogg

Still in college, programming as an intern. Couple years of experience with programming in general. Programmed so far in C#, Java, VB, and VBA.

Updated on September 18, 2022

Comments

  • Mike Kellogg
    Mike Kellogg over 1 year

    The Problem: A good number of people's info was added to my "Mail Contact" list in Exchange Management Console, but not all.

    Summary: I'm attempting to add a list of several hundred people from a excel .csv file into my Exchange Management Console (Mail contacts more specifically). A number of the contacts may Already be in the contact list which may have caused my foreach statement to stop running?

    My Question: Is there code I can run to check if the members exist then if not, add the member?

    I ran this code in powershell in this format:

    Import-Csv .\computech.csv | foreach { New-MailContact -Name $_.displayname -FirstName $_.firstname -LastName $_.lastname -ExternalEmailAddress $_.mail -OrganizationalUnit $_.OrganizationalUnit}

    Strangely enough it added a good number of the people from the list but not all of them. When I try to run the code now I get this error:

    Import-Csv : The member "Aaron" is already present.

    "Aaron" is the first member on the list and so the execution immediately stops there

    I had previously tried this using this code:

    [PS] C:\Windows\system32>Import-Csv \\FILLER\MailboxBackups\contacts.csv | foreach-object{if (Get-MailContact -External
    EmailAddress $_.ExternalEmailAddress){write-host $_.ExternalEmailAddress 'is a duplicate entry!!!!'} else {New-MailConta
    ct -Name $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName}}
    

    But I received the same "Aaron" is already present error lol. Any help would be greatly appreciated


    --------UPDATE------------ I found out that not all of the contacts were added because of Alias conflicts. Aliases were common names like "bob" which are sure to already exist. Do people normally use numbers to get around this, or use a users email address instead of simple names?

  • Mike Kellogg
    Mike Kellogg about 11 years
    if you read my question above, my last statement stated that I had previously tried this code and it didn't work. If you have insight as to why my version of it didn't work (as shown in my question) that could be helpful.
  • harrymc
    harrymc about 11 years
    The above uses the anr parameter for ambiguous name resolution search over: CommonName (CN), DisplayName, FirstName, LastName, Alias.
  • harrymc
    harrymc about 11 years
    @Mike: Please comment.
  • Mike Kellogg
    Mike Kellogg about 11 years
    didn't check the validity of this code because I haven't needed to use the function since. Will definitely try in the future.
  • Root Loop
    Root Loop about 9 years
    try '-ErrorAction SilentlyContinue' to skip duplicated entries.