Empty/clean up a group mailbox with EWS Managed API 2.2


There is not much to say here. Hope you'll find it useful! Feel free to share your opinions. 🙂

 

Prerequisites:

-Create a new RBAC group or use an existing one from Exchange Admin Center – Permissions – Admin Roles. Add the ‘ApplicationImpersonation’ role to the group and add as member the service account that will impersonate the mailbox that is a member/owner of the declared group mailbox (it can be your Global Admin account).

-The script requires EWS Managed API 2.2, which can be downloaded here: https://www.microsoft.com/en-gb/download/details.aspx?id=42951

 

 

DISCLAIMER: This application is a sample application. The sample is provided "as is" without warranty of any kind. Microsoft further disclaims all implied warranties including without limitation any implied warranties of merchantability or of fitness for a particular purpose.The entire risk arising out of the use or performance of the samples remains with you. in no event shall Microsoft or its suppliers be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss arising out of the use of or inability to use the samples, even if Microsoft has been advised of the possibility of such damages). Because some states do not allow the exclusion or limitation of liability for consequential or incidentaldamages, the above limitation may not apply to you.




$a = Read-Host -Prompt "Mailbox name"
$b = Read-Host -Prompt "Group mailbox"

Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"

$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList Exchange2013_SP1

#Provide the credentials 
$credential = Get-Credential -Message "Type the credentials of the mailbox with impersonation rights on $($a)"

$service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $credential.UserName, $credential.GetNetworkCredential().Password

#Exchange Online URL
$service.Url= new-object Uri("https://outlook.office365.com/EWS/Exchange.asmx")

#User to impersonate
$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$a)


$InboxFolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$b)

$CalendarFolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$b)

$DumpsterFolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::RecoverableItemsDeletions,$b)


$ItemView = new-object Microsoft.Exchange.WebServices.Data.ItemView(1000)

#Emtpy the Inbox folder

$FindItemResults = $service.FindItems($InboxFolderid,$ItemView)

if($FindItemResults.TotalCount) 
{

write-host "$($FindItemResults.TotalCount) item(s) have been found in the Inbox folder." -ForegroundColor White

$title1 = "Delete emails"

$msg1 = "Do you want to (soft) delete all the emails from your group mailbox ?"

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
    "Deletes all the emails from the folder."

$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
    "Retains all the emails in the folder."

$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

$result1 = $host.ui.PromptForChoice($title1, $msg1, $options, 0)

if(!$result1)

{

$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolderid)

$Inbox.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete,$null)

                   }
}

else { 
 
 Write-Host "No items were found in the Inbox folder" -ForegroundColor Yellow 
 
 }

#Move the calendar events in the dumpster  (The .Empty method is not supported for the Calendar folder)

do
{

$FindItemResults2 = $service.FindItems($CalendarFolderid,$ItemView)

if($FindItemResults2.TotalCount) 
{
write-host "$($FindItemResults2.TotalCount) item(s) have been found in the Calendar folder." -ForegroundColor White

$title2 = "Delete calendar events"

$msg2 = "Do you want to (soft) delete all the calendar events from your group mailbox calendar ?"

$result2 = $host.ui.PromptForChoice($title2, $msg2, $options, 0)

if(!$result2)

{

foreach ($Item in $FindItemResults2.Items)
{
$Calendar = [Microsoft.Exchange.WebServices.Data.Appointment]::Bind($service,$Item.Id)

$Calendar.Move($DumpsterFolderid)
}
                   }
}
        else { 
               Write-Host "No items were found in the Calendar folder" -ForegroundColor Yellow 
             }

$ItemView.offset += $FindItemResults.Items.Count

}while($FindItemResults.MoreAvailable -eq $true)

 

 

Comments (0)

Skip to main content