Exchange: Delete Emails with Search Terms

If you need to delete emails through the Exchange shell, you try to the following command.  This command will delete any email that is older than January 1st, 2015.

search-mailbox -id user1 -SearchQuery "Received < 01/01/2015" -DeleteContent -Force

If you want to make a copy of the deleted emails, you can try the following command

search-mailbox -id user1 -SearchQuery "Received < 01/01/2015" -TargetMailbox user2 -TargetFolder "Deleted Items" -DeleteContent -Force

Due to the limitation of this script, it can do perform 10,000 emails at a time. I come up with a loop to make it re-run 15 times.

$mycounter=1
do
{
  $mycounter++
  search-mailbox -id user1 -SearchQuery "Received < 01/01/2015" -TargetMailbox user2 -TargetFolder "Deleted Items" -DeleteContent -Force
}
until ($mycounter -ge 15)

 

Comments are closed