PowerShell: Purge folder of items over a certain age

This PowerShell script can be used to purge a folder of its contents, optionally only deleting items before a certain date.

Example usage:

.\Purge-MailboxFolder user1.ex2k10@hybrid.local “Test Folder” “02/Aug/2013” -Impersonate -SearchForFolder

The above will search for the first folder called Test Folder in the mailbox of user1.ex2k10@hybrid.local, and when found it will delete all messages dated before 2nd August 2013.  It uses impersonation to access the mailbox.


Full parameter details:

Purge-MailboxFolder -Mailbox <string>
                    -Folder <string>
                    -PurgeBeforeDate <DateTime>
                    [-Username <string> -Password <string> [-Domain <string>]]
                    [-EwsUrl <string>]
                    [-EWSManagedApiPath <string>]

 -Mailbox : Mailbox SMTP email address (OR filename for source list of mailboxes; text file, one mailbox per line)
 -Folder : Folder to be purged (items within it deleted).  Full path must be specified if SearchForFolder parameter is missing.
 -PurgeBeforeDate : The date before which items will be deleted from the folder

 -SearchForFolder : If present, the mailbox will be searched for until the named folder is found
 -Username : Username for the account being used to connect to EWS (if not specified, current user is assumed)
 -Password : Password for the specified user (required if username specified)
 -Domain : If specified, used for authentication (not required even if username specified)
 -Impersonate : If set, impersonation will be used
 -EwsUrl : Forces a particular EWS URl (otherwise autodiscover is used, which is recommended)
 -IgnoreSSLCertificate : If set, any SSL errors will be ignored
 -EWSManagedApiPath : Filename and path to the DLL for EWS Managed API (if not specified, paths are searched)


Comments (5)

  1. Sankar Munirathinam says:

    Thanks you… Using this script i could delete the items in the specified folder. How can i delete the folder itself?

  2. Sankar Munirathinam says:

    Thanks, This script is helpful to us.. Using this script we could delete the items from the specified folder. But we need to delete the folder itself. How can we delete the folder itself including all the items?

  3. Greg Morris says:

    Great Script!  

    Is there any way to modify the script to allow it to recursively work on messages contained in the specified folder & its sub-folders?  

  4. Saaj says:

    Awesome, works great!

    Make sure you add impersonation role and use -impersonate switch if running against mailboxes other than your own, else emails will be deleted from your own mailbox.

    New-ManagementRoleAssignment –Name: <ImpersonationAllowed> –Role:ApplicationImpersonation –User: <administrator>


  5. David says:

    This script worked great for the root of the folder chosen. However, is there a way to make it remove messages from the selected folder as well as the sub-folders within it?

Skip to main content