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!

 

 

 

Twan van Beers

Twan is a senior consultant with over 20 years of experience. He has a wide range of skills including Messaging, Active Directory, SQL, Networking and Firewalls. Twan loves to write scripts and get deep and dirty into debugging code, in order to understand and resolve the most complex of problems.

This Post Has 8 Comments

  1. Hi Twan,

    Thanks for the great script…i’m in the same situation i tried many other tools with no luck.

    Just download the script and tried to run it but i’m getting this error “Missing or invalid array index expression.”

    Do you know what i’m missing?? Running it en Exchange 2010 SP3

    Thanks in advance

    1. Hi Jorge,

      I found an issue with the code pasted on the MS site (the download file should be fine)

      Change the line $AllSubFolders[$].Delete to $AllSubFolders[$i].Delete

      Cheers
      Twan

      1. Hi Twan,

        The script worked fantastic! I was able to delete those nested Folders/subfolders (1200+ Folders) after testing many tools.

        It was great…Awesome! Thanks again for your quick support and collaboration

        Cheers,
        Jorge

  2. Hi Twan,
    I am getting the following errors when running the script-

    Exception calling “AutodiscoverUrl” with “2” argument(s): “The Autodiscover service couldn’t be located.”
    At C:\MyO365Tennant\Myo365scripts\Deleting Outlook Folders Using EWS\DeleteFoldersBottomUp (2).ps1:13 char:1
    + $Service.AutodiscoverUrl($mbxName, {$True})
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : AutodiscoverLocalException

    Exception calling “Bind” with “2” argument(s): “The Url property on the ExchangeService object must be set.”
    At C:\MyO365Tennant\Myo365scripts\Deleting Outlook Folders Using EWS\DeleteFoldersBottomUp (2).ps1:16 char:1
    + $RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service, $Root …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServiceLocalException

    You cannot call a method on a null-valued expression.
    At C:\MyO365Tennant\Myo365scripts\Deleting Outlook Folders Using EWS\DeleteFoldersBottomUp (2).ps1:23 char:1
    + $Response = $RootFolder.FindFolders($SearchFilter, $FolderView)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    1. Hi Bosco,

      This means that the machine running the script is not able to autodiscover Exchange based on the mailbox name entered in the script (first few lines). Is that a valid mailbox name on your Exchange server?

      Regards
      Twan

    2. I have the same problems with autodiscover.

      It’s an valid email but it’s shared mailbox if that have something to do with it?

      Could you manually input the correct server if you look it up? 🙂

      1. Hi Frederic,

        A shared mailbox should really make a difference. You can replace the line
        $Service.AutodiscoverUrl($mbxName, {$True})
        with
        $Service.AutodiscoverUrl('http://autodiscover.xxx/autodiscover/autodiscover.xml')

        Regards
        Twan

Leave a Reply

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

Search