Share via


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.<br>$a = Read-Host -Prompt "Mailbox name"<br>$b = Read-Host -Prompt "Group mailbox"<br>Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll"<br>$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<br>$service.Url= new-object Uri("https://outlook.office365.com/EWS/Exchange.asmx")<br>#User to impersonate<br>$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$a)<br>$InboxFolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox,$b)<br>$CalendarFolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$b)<br>$DumpsterFolderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::RecoverableItemsDeletions,$b)<br>$ItemView = new-object Microsoft.Exchange.WebServices.Data.ItemView(1000)<br>#Emtpy the Inbox folder<br>$FindItemResults = $service.FindItems($InboxFolderid,$ItemView)<br>if($FindItemResults.TotalCount) <br>{<br>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)



 {<br>$Inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$InboxFolderid)<br>$Inbox.Empty([Microsoft.Exchange.WebServices.Data.DeleteMode]::SoftDelete,$null)<br>                   }<br>}<br>else {   Write-Host "No items were found in the Inbox folder" -ForegroundColor Yellow   }<br>#Move the calendar events in the dumpster  (The .Empty method is not supported for the Calendar folder)<br>do<br>{<br>$FindItemResults2 = $service.FindItems($CalendarFolderid,$ItemView)<br>if($FindItemResults2.TotalCount) <br>{<br>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)

 {<br>foreach ($Item in $FindItemResults2.Items)<br>{<br>$Calendar = [Microsoft.Exchange.WebServices.Data.Appointment]::Bind($service,$Item.Id)<br>$Calendar.Move($DumpsterFolderid)<br>}<br>                   }<br>}<br>        else { <br>               Write-Host "No items were found in the Calendar folder" -ForegroundColor Yellow <br>             }<br>$ItemView.offset += $FindItemResults.Items.Count<br>}while($FindItemResults.MoreAvailable -eq $true)