PowerShell/EWS: Searching for and deleting duplicate mailbox folders No ratings yet.

We’ve had some cases recently where an issue with migration has caused the migrated mailbox to contain duplicate folders (e.g. there may be two Calendar folders).  I have written a script that can process mailboxes and automatically remove duplicate folders.  In the case of special folders (Inbox, Sent Items, etc.) the correct folder will be retained.  For other folders, just one will be kept (though which one doesn’t matter – the first will be chosen).  Any folders to be deleted will have the items (and folders) they contain moved to the other folder of the same name.  Once the script has run, you should end up with a mailbox with no duplicate folder names.

This script requires an Exchange Management Session.  You can import this into the PowerShell session before running this script, run it from Exchange Management Shell, or specify -PowerShellUrl (with valid credentials using -Credentials) for the script to attempt to connect and import the session itself.  For automatic fixing, the script also requires the EWS Managed API to be downloaded and installed.  The actual folder moving/merging is handled by another script that needs to be downloaded and placed in the same folder as this one (or the current folder of a PowerShell session).  You can obtain the Merge-MailboxFolder.ps1 script here.  If you just want a report on duplicate folders, then you won’t need the Merge-MailboxFolder.ps1 script (or to install the EWS Managed API) and can just run the script as per example 1, 2 and 4.

By default the script will not make any changes to a mailbox.  You need to specify the -Repair parameter to attempt to fix duplicate folders.

A folder is only considered to be a duplicate if there is another folder with the same name and at the same folder level in the mailbox (i.e. a folder has two subfolders with the same name).  Folders with the same name, but with different parent folders, are not considered duplicates.

If you want to process multiple mailboxes using an admin account, and you are not just generating a report (i.e. are using -Repair), then you’ll need to ensure that your admin account has been granted ApplicationImpersonation permissions so that it can access the mailboxes to resolve the duplicate folders (see here for more information on this)..

Example 1: check for duplicate folders in on-premises mailbox (but do not make any changes)

$c = Get-Credential
.\Fix-DuplicateMailboxFolders.ps1 -Mailbox 1@e15.local -PowerShellUrl “http://cas1.e15.local/PowerShell” -Credentials $c

This connects to an on-premises Exchange server to import an Exchange PowerShell session (using the given credentials), and then searches for duplicate folders in the mailbox of 1@e15.local.  No changes are made to the mailbox as the -Repair parameter is not specified.

Example 2: check for duplicate folders in an Office 365 mailbox (but do not make any changes)

$c = Get-Credential
.\Fix-DuplicateMailboxFolders.ps1 -Mailbox 1@demonmaths.co.uk -Credentials $c

Example 3: check for and repair duplicate folders in an Office 365 mailbox, and log any changes to specified log file

$c = Get-Credential
.\Fix-DuplicateMailboxFolders.ps1 -Mailbox 1@demonmaths.co.uk -EwsUrl “https://outlook.office365.com/EWS/Exchange.asmx” -Credentials $c -LogFile “c:\temp\log.txt” -Repair

The -EwsUrl is specified as all Office 365 mailboxes have the same EWS Url.  Specifying the Url as a parameter here speeds up the script as it bypasses the need for autodiscover.

Example 4: check for duplicate folders in all Office 365 mailboxes and log results (but do not make any changes to any of the mailboxes)

$c = Get-Credential
.\Fix-DuplicateMailboxFolders.ps1 -Credentials $c -LogFile “c:\temp\log.txt”


Download Fix-DuplicateMailboxFolder script


Please rate this

Leave a Reply

Your email address will not be published. Required fields are marked *