Search and Delete messages in Exchange Online between dates
A few days ago I recieved a question out of the usual: How to delete messages from a mailbox between two dates without the need of applying retention policies.
Of course, this can´t be done from any admin console, but it can be done from PowerShell keeping in mind the following requisites:
- Have our PowerShell environment correctly configured to connect our Office365 subscription.
- Make sure the user we use is a member of the Security Role «Discovery Management».
- Create a Security Role in Exchange Online (if don´t have it yet) that includes the «Mailbox Import Export» role.
- Make our user a member of this Role.
Once we met the requisites, connect to the office365 subscription with the following CmdLets:
$LIveCred = Get-Credential $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LIveCred -Authentication Basic -AllowRedirection Import-PSSession $Session
Finally, once inside run the following CmdLet:
Search-Mailbox usuario@dominio.com -SearchQuery «Received: $(‘mm/dd/yyyy’) and Received:< $(‘mm/dd/yyyy’)» -DeleteContent
This will Soft-Delete the content between these dates.
Hope you find this useful.