Deleting Folders

Deleting Folders

from an Exchange Mailbox with PowerShell

I was given an escalation at a client where somehow a user managed to create thousands of folders all nested one inside the other.  So the task was how to delete these folders.

User and 1/2/3rd line support had already tried using Outlook, OWA and MFCMapi but none of them worked for the user anymore (plus personally the thought of using MFCMapi makes me shudder…)

First thought, surely you can just delete them with PowerShell…?  Alas, there is no Remove-MailboxFolder, nor does eDiscovery help with the search and destroy feature as this is only for mail, not folders.

Right so what is left…?  EWS to the rescue, however the thought of doing EWS with PowerShell did make me think twice.  I did a quick search on Google and found Delete Outlook Folders Bottom-Up.  To make it run you need the EWS Managed API, which can be downloaded from the Microsoft site.  The current version at time of writing is v2.2 found here

I took a quick look through the code though and found that actually it isn’t paging through the results properly and so won’t work for more than 1000 folders.  So I set about fixing it.

First was to add paging which was surprisingly simple.  The following code snippet shows how the paging works by adjusting the view’s offset

$MoreResults = $true
do{
   $Response = $RootFolder.FindFolders($FolderView)
   $AllSubFolders += $Response.Folders
   $MoreResults = $Response.MoreAvailable
   $FolderView.Offset += $Response.Folders.count
} while( $MoreResults -eq $true )

Then deleting the folders from the bottom up.  Since I have all of the folders in an array and the folders are pulled out in the order they were created (therefore the parent folder always exists before the lower level one), we can simply reverse our way through the array and delete them.

for( $i = ($AllSubFolders.Count - 1); $i -gt 0; $i-- ) {
   $AllSubFolders[$i].Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
}

Even after doing those two pieces I struggled because the sheer number of folders meant that EWS was timing out.  Not to fear since we can catch that!

for( $i = ($AllSubFolders.Count - 1); $i -gt 0; $i-- ) {
   $error.clear()
   $AllSubFolders[$i].Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)

   if( $error[0] -like '*the operation has timed out*' ) { $i++ }
}

Finally I didn’t like how the original code tried to find the top level folder so changed it from looping through all folders to just searching for the one with the right display name

$SearchFilter = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo( [Microsoft.Exchange.WebServices.Data.FolderSchema]::Displayname, $topFolderName )
$Response = $RootFolder.FindFolders($SearchFilter, $FolderView)

Putting it all together and we get a pretty cool (albeit dangerous) script.  Since the original script was on MS Technet Gallery I uploaded this revision there too.  Delete Outlook Folders Bottom-Up (v2)

The bottom line for me was that actually using EWS from Powershell isn’t actually that difficult, and adds some very powerful tools to the Exchange Admin’s toolbelt

Enjoy!

 

 

 

Related posts: