Sometimes it is necessary to find and delete duplicates within a Microsoft Exchange or Office 365 mailbox. For example, when a clean Migration has been run within Migration Monster without first deleting and recreating or emptying the destination mailbox.
The following PowerShell script and procedure can assist with this:-
Please check all links for the latest versions of scripts and procedures as from time to time they are updated.
We have also attached the script it self as an attachment but again, please check for the latest versions.
The script was written by Michel de Rooij https://social.technet.microsoft.com/profile/michel%20de%20rooij/
Step 1: Have to have version 5 of PowerShell installed
You'll need to confirm you have Powershell version 5 running for this work.
To check your version of Powershell type in $psversiontable
Here's a link for what's needed to install the most current version -
Step 2: Need to make sure to have Microsoft Exchange Web Services Managed API 2.2 installed
Step 3: Need to install Microsoft Online Services Sign-In Assistant for IT Professionals RTW
Step 4: Need to install PackageManagement PowerShell Modules Preview - March 2016
Step 5: You'll need to configure Powershell connection to Office 365 (if applicable)
Step 6: Setup impersonation of the mailbox you will be administering i.e. removing the duplicates from
Syntax and examples below but please also see the latest updates at:- https://gallery.technet.microsoft.com/office/Removing-Duplicate-Items-f706e1cc
.\Remove-DuplicateItems.ps1 -Mailbox <mailbox.org> -Credentials $Credentials -impersonation -includefolder '#inbox#\*','#calendar#\*' -MailboxWide -verbose -server outlook.office365.com
Remove-DuplicateItems.ps1 [[-Identity] <String>] [[-Type] <String>] [-Retain <String>] [-Server <String>] [-Impersonation] [-DeleteMode <String>] [-Credentials <PSCredential>] [-Mode <String>] [-MailboxOnly] [-ArchiveOnly] [-IncludeFolders <String>] [-ExcludeFolders <String>] [-PriorityFolders <String>] [-MailboxWide] [-NoSize] [-NoProgressBar] [Report] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]
.\Remove-DuplicateItems.ps1 -Mailbox Francis -Type All -Impersonation -DeleteMode SoftDelete -Mode Quick -Verbose
Check Francis' mailbox for duplicate items in each folder, soft deleting duplicates, matching on PidTagSearchKey and using impersonation.
.\Remove-DuplicateItems.ps1 -Mailbox Philip -Retain Oldest -Type Mail -Impersonation -DeleteMode MoveToDeletedItems -Mode Full -Verbose
Check Philip's mailbox for duplicate task items in each folder and moves duplicates to the Deleted Items folder, using preset matching criteria and impersonation. When duplicates are found, the oldest is retained.
.\Remove-DuplicateItems.ps1 -Mailbox firstname.lastname@example.org -Credentials $Credentials
Sets $Credentials variable. Then, check email@example.com's mailbox for duplicate items in each folder, using Credentials provided earlier.
.\Remove-DuplicateItems.ps1 -Mailbox firstname.lastname@example.org -Server outlook.office365.com -Credentials $Credentials -IncludeFolders '#Inbox#\*','\Projects\*' -ExcludeFolders 'Keep Out' -PriorityFolders '*Important*' -MailboxWide
Remove duplicate items from specified mailbox in Office365 using fixed Server FQDN - bypassing AutoDiscover, limiting operation against the Inbox, and top Projects folder, and all of their subfolders, but excluding any folder named Keep Out. Duplicates are checked over all folders, but priority is given to folders containing the word Important, causing items in those folders to be kept over items in other folders when duplicates are found.