Updating User properties using GraphAPI

Powershell Script to Remove Email from dumpster folders, Purges and Deletions

Sometimes a situation could arrive to clear items from the dumpsters hidden folders from a mailbox, as these folders are hidden so items from these folders can't be removed from outlook.
Though we can use MFCmapi to access these folders but that method is not easy to use and preferable if we want to remove items quickly or from multiple user mailboxes.

NOTE: If you looking for a way to take a backup of the email in purges and deletions folders, before you delete them for forever. use my script on the same here to export the email as .eml file to your machine.

Clearing Items from dumpsters

Dumpsters folder can be accessed under: Recoverableitemsroot
Below are folder available under: Recoverableitemsroot
  • Audits
  • Calendar Logging
  • Deletions
  • Purges
  • Versions 
In this script, we will see how can we clear Deletions and Purges folder.
As we are going to use the EWS make sure you met all the prerequisites before you plan to run this script.
  • Admin account should have application impersonation rights, you can follow this msdn post to setup the permissions.
  • Install EWS managed API on system where you plan to run the script from, if API is not installed, download the same from here
Note: Please install the latest version of EWS API, version 2.2 is currently being used in my script.

Download the script from GitHub

#===================================================================================================
#Author = Sunil Chauhan
#Email= Sunilkms@gmail.com
#Blogs=Sunil.chauhan.blogspot.com
#=====================================================================================================
# this Script can be useful to Quickly Empty Hidden Folders in user mailbox
# Like deletions and purges and others folders
#Usage Example:
#Empty Recover Deleted Items Folder From user Mailbox "Testuser@xzy.com"
#>.\ClearDumpstersItems-Ews.ps1 -Mailbox "Testuser@xzy.com" -Deletions:$true -admin "admin@xyz.com" -pass "adminpass"

#Empty purges Folder From user Mailbox "Testuser@xzy.com"
#>.\ClearDumpstersItems-Ews.ps1 -Mailbox "Testuser@xzy.com" -Purges:$true -admin "admin@xyz.com" -pass "adminpass"
#=====================================================================================================

param (
  $Mailbox,
  $Deletions=$false,
  $Purges=$false,
  $admin,
  $pass
      )

#Impersonate Admin Account details
$AccountWithImpersonationRights=$admin
$password=$pass
#$AccountWithImpersonationRights=$admin
#$password=$pass

#folder which you wants to empty from user mailbox
$FolderToEmpty=$folder

#EWS url for your orgnization
$uri=[system.URI] "https://outlook.office365.com/ews/exchange.asmx"
#define EWS Dll file Path
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
Import-Module $dllpath
#-------------------------------------------------------------------------------------
## Set Exchange Version
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList `
$AccountWithImpersonationRights, $password
$service.url = $uri
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId `
([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SMTPAddress,$Mailbox);
$MailboxRootid= new-object Microsoft.Exchange.WebServices.Data.FolderId `
        ([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Recoverableitemsroot,$ImpersonatedMailboxName)
$MailboxRoot=[Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$MailboxRootid)
$FolderList = new-object Microsoft.Exchange.WebServices.Data.FolderView(100) 
$FolderList.Traversal = [Microsoft.Exchange.WebServices.Data.FolderTraversal]::Deep
$findFolderResults = $MailboxRoot.FindFolders($FolderList)

if ($Deletions) {

 $Deletions = $findFolderResults | ? {$_.DisplayName -eq "Deletions"}
 Write-host "Item will be deleted from folder:" $Deletions.DisplayName
 Write-host "No of Items in Folder:"$Deletions.TotalCount
 Read-host "Hit enter to Remove all the items..."
 #$Deletions.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete,$False)
 $Deletions.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete,$True)
    "Done"
}

if ($Purges) {

 $Purges=$findFolderResults | ? {$_.DisplayName -eq "Purges"}
 Write-host "Item will be deleted from folder:" $Purges.DisplayName
 Write-host "No of Items in Folder:"$Purges.TotalCount
 Read-host "Hit enter to Remove all the items..."
 #$Purges.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete,$False)
 $Purges.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete,$True)
    "Done"
}

Comments