Powershell Needful Things put that in your pipeline

31May/1020

Find and delete duplicate Outlook Contacts

I have been fairly busy at work with little or no time to write. I did however manage to write a neat script on Friday which I thought I had to share.

My Nokia decided last week that it felt the need to duplicate all my Outlook contacts after I changed something on the ActiveSync profile. Now, normally this is not a big deal, as you can simply sort the contact items by creation date, and delete the duplicates, that is unless you mess around with them, and recreate them all from scratch. (insert curse word here)

I took one look at this problem and thought that it would be far too easy to just delete them manually. I decided to write a script to do the work for me. I have been playing a lot with the Outlook COM object lately so I already had most of the code to get this done quickly.

The script will collect all your contacts, and do a unique sort on the FullName. It then creates a temp folder under your default contacts folder, and moves the unique contacts (remember sorted by FullName only), to the temp folder.

It then dumps all the duplicates in the default contacts to a CSV and deletes them from the contacts.

At this point I stopped the script, as it made sense to check the CSV and the temp folder, and move your contacts back manually if you are happy with the results.

As usual, be very careful with this one. Automatic deletes always have the potential to end in tears. Make a backup of all your contacts before you start with the script.

I hope this script can help you.

You can download the script from here:

Comments (20) Trackbacks (1)
  1. Hi, I need this script, can you send to me ?

    Regards

  2. Thanks! I had a user with an iPad that went nuts trying to synch between Outlook and his Samsung smart phone and he ended up with 217,996 contacts. I am running your script now to clean this mess up.

  3. We have a very real need for a script such as this, so thank you for freely sharing your solution. We get the following error though …

    You cannot call a method on a null-valued expression.
    At c:\clean-contacts.ps1:20 char:14
    + $Contact.Move <<<< ($tempFolder) | foreach-object {Write-Progress "Backup unique items to temp folder…" $_.FullName; $_.FullName} | Out-Null

    • Keith,

      Thanks for downloading! Outlook must be logged on as the user with the problem when you run the script.

      Also, please send me a screen shot of the problem if you could to jeanlouw at gmail dot com.

      I will gladly have a look for you.

      Jean

    • Another thing, you have to change this line:

      ‘$olSession.Logon(‘Outlook’)’

      To have the profile name of your problem user. This tells the script which user to logon as.

      Keep me posted.

      Jean

  4. I’ve got a user with an Android phone that went crazy and as of today has 465,000 contacts (only ~300 unique). I’m currently running the script now. If this works I’ll be incredibly happy! It’s sitting at “…getting unique items”. Any idea approximately how long it will take to process?

    Thanks again for sharing this!

    -R

    • Ryan,

      Thanks for downloading. When I used the script for my problem, I only had a couple of thousand duplicates. I know others have tried the script with larger contact folders, and the problem is that the powershell.exe process may run out of memory.

      As far as how long this takes, I cannot say, doing a unique sort on a list of almost half a million items is a tall order.

      Just watch the memory usage, and if it works let me know how long it took.

      Good Luck!

      Jean

  5. Great Script! However, I can’t seem to delete the temp folder that was created. I don’t have permissions to. Any ideas?

  6. Dumb question – how do I run your script on windows 7?

  7. dumb question – how do I run your script on windows 7, I downloaded it but it open in a txt file? sorry to be so dumb

  8. Hello,

    I try to delete all my contacts and tried with it based on your script:

    $olSession = (New-Object -ComObject Outlook.Application).Session
    $olSession.Logon(‘Outlook’)
    $contactsFolder = 10
    $elemek = $olSession.GetDefaultFolder($contactsFolder).Items
    foreach ($Contact in $elemek) { $Contact.Delete() }

    Unfortunatelly the script delete only the half of my contacts. If i run again, delete the half again…

    Any idea why quit the from the loop at half way?
    Thank you.

  9. You don’t by chance have something like this to delete calendar entries do you?


Leave a comment