Delete Outlook Folders Bottom Up (v2)

I started with the post from Nuno Mota with the same title to try to delete over 8000 nested folders from a user's mailbox.  They were all in Deleted Items and nested very very deeply.  Outlook no longer worked, nor did OWA and neither did MFCMAPI.  Unfortunately since I needed

1,359 times
Add to favorites
E-mail Twitter Digg Facebook
  • Need to search a mailbox
    2 Posts | Last post April 22, 2017
    • Looking for a script to search a specific folder in all mailboxes, forward any emails in that folder to an email address, and then delete the email from the folder. This needs to be done in Exchange online. Any chance you have anything like this already?
    • Hi Ken,
      In Exchange Online you can use the Security and Compliance portal to search for mail from a certain folder across multiple mailboxes.  Once found then you can copy those to a nominated target mailbox.  I don't think you'd need to write anything as a custom script for this.
  • Autodiscover error
    3 Posts | Last post September 22, 2016
    • Hi,
      I am running this script using O365 and I receive an error regarding the Autodiscover (Please see it below). How can I set the Autodiscover in the script to AllowRedirection?
      Exception calling "AutodiscoverUrl" with "2" argument(s): "The Autodiscover service returned an error."
      At D:\Script to delete folders in a Mailbox\DeleteFoldersBottomUpAndyTest.ps1:13 char:1
      + $Service.AutodiscoverUrl($mbxName, {$True})
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : AutodiscoverRemoteException
      Exception calling "Bind" with "2" argument(s): "The Url property on the ExchangeService object must be set."
      At D:\Script to delete folders in a Mailbox\DeleteFoldersBottomUpAndyTest.ps1:16 char:1
      + $RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Ser ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
          + FullyQualifiedErrorId : ServiceLocalException
      You cannot call a method on a null-valued expression.
      At D:\Script to delete folders in a Mailbox\DeleteFoldersBottomUpAndyTest.ps1:23 char:1
      + $Response = $RootFolder.FindFolders($SearchFilter, $FolderView)
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
          + FullyQualifiedErrorId : InvokeMethodOnNull
      Thank you in advance,
    • Hi Andy,
      The code already does set the AllowRedirection to True, so it would seem like the email address passed can't do a successful autodiscover for some reason.  Have you tried to download the client from (i.e. don't run the test directly from the website) and running an autodiscover test locally on your machine?
    • Hi Twan,
      I used the client tool as you suggested and the results were all with the "Expected Value". The online test fails the two first steps and the third was successful. Please see them below:
      Attempting to test potential Autodiscover URL
      Testing of this potential Autodiscover URL failed.
      Attempting to test potential Autodiscover URL
      Testing of this potential Autodiscover URL failed.
      Attempting to contact the Autodiscover service using the HTTP redirect method.
      The Autodiscover service was successfully contacted using the HTTP redirect method.
      Attempting to test potential Autodiscover URL
      Testing of the Autodiscover URL was successful.
      What do you think could be the issue? I will really appreciate your suggestions.
  • too many folders possibly?
    3 Posts | Last post June 22, 2016
    • Sorry to bother, so far for a couple of smaller mailboxes it's worked fine. i have a trouble mailbox that has over 22,000 folders with over 18,000 of them under a single root folder. when i run the script the $topfoldername isn't being found. could that be there are too many folders and it's not found in the first set of those returned? i ran it in debug mode and the $response variable is null.
      thanks again.
    • Hi Jack,
      As long as the top level folder is in the first 1000 folders created with the mailbox then it will find it.  If however it is a much later addition then the script would need to become a bit more complicated...
      We would need to wrap the FolderView setup and searching for the folder with some paging to traverse through all the folders and not just the first 1000 created.
    • Actually try increasing the page size to say 10000 (might be simpler)
      $FolderView = New-Object Microsoft.Exchange.WebServices.Data.FolderView(10000) 
      $FolderView.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep 
  • leaving one folder in place
    3 Posts | Last post June 21, 2016
    • thanks for the script, i have a problematic mailbox with over 20,000 folders and was looking for a solution. I've been testing with a test mailbox that is much smaller and it appears to leave the first folder under the root in place. I've been working through the code and it looks like the final loop will not hit the last folder.
      can you tell me if i'm missing something?
    • changed this line to -ge and it worked. 
      for( $i = ($AllSubFolders.Count - 1); $i -ge 0; $i-- ) 
      thanks again.
    • Thanks Jack,
      Yes your change is the right one.  I didn't want to delete the top level folder as well, so left it as -gt to avoid that folder being deleted
      Glad you solved it for your requirement and found it useful