PowerShell: Update folders (delete/purge, or add/delete properties)

Please note: the script has now been moved to https://code.msdn.microsoft.com/PowerShell-Update-mailbox-1e17866d.

This script allows you to update folders by adding or removing properties.  You can also use the script to purge or delete the folders.

To delete properties from a folder:

.\Update-Folders.ps1 -Mailbox 1@e14.local -FolderPath "\Folder 1" -ProcessSubFolders -DeleteFolderProperties @("0x36160102", "0x36DA0102")

In the above example, MAPI properties 0x36160102 and 0x36DA0102 will be deleted from Folder 1 (which is at the same level as Inbox i.e. a subfolder of the mailbox root message folder) and all subfolders.

To add properties to a folder:

.\Update-Folders.ps1 -Mailbox 1@e15.local -FolderPath "Inbox\Test" -AddFolderProperties @{"0x3004001F" = "This is the folder comment"}

In the above example, a folder comment is added to the folder Test which is found in the Inbox.

Note that -DeleteFolderProperties requires an array (or some other enumerable list) as input, so is defined above with normal () bracket.  -AddFolderProperties requires a hash table (a dictionary object would also work), so is defined with curly brackets {}.

Script parameters:

 -Mailbox  The mailbox to be processed (if missing, current user's mailbox is assumed, though this will only work in a domain environment).
 -PublicFolders  If this switch is present, public folders will be processed.
 -Archive  When specified, the archive mailbox is accessed (instead of the main mailbox).
 -FolderPath  If specified, processing will start from the named folder.  If not specified, the message root folder (or public root) is assumed.
 -ProcessSubfolders  When specified, subfolders will also be processed.
 -AddFolderProperties  Adds the given properties (must be supplied as hash table @{}) to the folder(s).
 -DeleteFolderProperties  Deletes the given properties from the folder(s).
 -Delete  Deletes the folder(s) - this will only work if they are empty.
 -Purge  Purges (empties) the folder(s).  This parameter is required if you want to delete folders that have messages in them.
 -Credentials  Credentials used to authenticate with EWS.
 -Username  Username used to authenticate with EWS.
 -Password  Password used to authenticate with EWS.
 -Domain  Domain used to authenticate with EWS.
 -Impersonate  Whether we are using impersonation to access the mailbox.
 -EwsUrl  EWS Url (if omitted, then autodiscover is used).
 -EwsManagedApiPath  Path to managed API (if omitted, a search of standard paths is performed).
 -IgnoreSSLCertificate  Whether to ignore any SSL errors (e.g. invalid certificate) - use with care.
 -AllowInsecureRedirection  Whether to allow insecure redirects when performing autodiscover.
 -LogFile  Log file - activity is logged to this file if specified.
 -Trace  If specified, EWS requests/responses will be dumped to the standard output (usually console)
Comments (9)

  1. Andyswe says:

    My goal is to use your script to delete all mail in Deleted Items for all users but I've banged my head against the wall long enough here and would really appriciate a simple how-to:

    So I've downloaded and installed the Exchange Web Services v2.2, imported the Microsoft.Exchange.WebServices.dll into the current session, I'm logged on as admin with full access to the mailbox I'm trying to purge mail from. Running:

    .Update-Folders.ps1 -FolderPath "Deleted Items" -ProcessSubFolders -Purge ; Works fine for current user (Administrator)

    .Update-Folders.ps1 -Mailbox "given.surname" -FolderPath "Deleted Items" -ProcessSubFolders -Purge -Impersonate -ewsurl "mail.contoso.com/…/exchange.asmx" ; Does not work, I get "The SMTP address format is invalid.".

    What am I missing?

  2. Mailbox parameter needs to be the SMTP address of the mailbox.  Also, application impersonation needs to be set up for your admin account (in case you get a further error once you change to SMTP address – if you've already configured impersonation, you won't).

  3. Dev says:

    Can you help me out with a powershell script to disable outlook addins on runtime?

  4. Frederick says:


    I came across this script and it seems Fantastic!

    I would like to use your script to delete a specific folder (i.e. Marketing) from about 1000 users.  Can I call a txt or csv file with your script.  If so can you please give me an example for the PS command also the file that will be called.  I appreciate your help

  5. The -Mailbox parameter can take a text file as a parameter.  In that text file, list the mailboxes that need to be processed (one per line, and it must be the primary SMTP address of the mailbox).  You'll need to ensure that the authenticating account has permission to access the mailbox (impersonation is best).

  6. Bill Rogge says:

    So I am trying to utilize this script as well (very poor at PowerShell) and keep getting a strange error.

    PS C:Usersissupportdesktop> .Update-Folders.ps1 -Mailbox "wrogge" -FolderPath "Spam" -ProcessSubFolders -Purge -Impersonate -ewsurl "D:Program FilesMicrosoftExchange ServerV15ClientAccessexchwebews" ;

    Security warning

    Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your computer. Do you want to run C:UsersissupportdesktopUpdate-Folders.ps1?

    [D] Do not run  [R] Run once  [S] Suspend  [?] Help (default is "D"): R

    Using managed API 15.00.0516.014 found at: C:Program FilesMicrosoftExchangeWeb Services2.0Microsoft.Exchange.WebServices.dll

    Processing mailbox wrogge

    Exception calling "Bind" with "2" argument(s): "Protocol file isn't supported for service requests."

    At C:UsersissupportdesktopUpdate-Folders.ps1:587 char:13

    +             $rootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($gl …

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

       + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

       + FullyQualifiedErrorId : ServiceLocalException

    Unable to bind to root folder.  No further processing possible

    PS C:Usersissupportdesktop>

    Checking line 587 it says " $rootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($global:service, $folderId)"

    It appears that for some reason, it is not able to evaluate that folder (which does exist).

    Could the issue be that there are sub-folders within this root level folder?

  7. Anatoliy Makarevych says:

    Great script, Dave – much appreciate your work, man. That helped a lot to do my work!

  8. Andy B says:

    I am running the following command as I would like to delete folders out of deleted items but am getting the error any thoughts?

    .Update-Exchange-Folders.ps1 -Mailbox emailaddress -FolderPath "Deleted

    ItemsManaged Folders" -Delete -ProcessSubfolders

    Using managed API 15.00.0913.015 found at: C:Program FilesMicrosoftExchangeWeb Services2.2Microsoft.Exchange.WebServices.dll

    Processing mailbox such and such

    Exception calling "Bind" with "2" argument(s): "The Url property on the ExchangeService object must be set."

    At C:Program FilesMicrosoftExchange ServerV15ScriptsUpdate-Exchange-Folders.ps1:587 char:13

    +             $rootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($gl …

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

       + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

       + FullyQualifiedErrorId : ServiceLocalException

    Unable to bind to root folder.  No further processing possible

  9. Stephen Miserez says:

    Hi All,

    I am trying to delete certain folders with the script. Deleting empty folders is working with no issues, however when trying to delete the items first using the -purge command, we receive “Failed to purge FolderName”.

    Running the following command:
    .\Update-Folders.ps1 -Mailbox “smtpaddress” -FolderPath “Testing123” -Purge -Verbose
    This is does work for my current admin account which I am running it as or against other mailboxes with the -Impersonate command.

    Thank you,
    Stephen Miserez

Skip to main content