Search-Mailbox - date range deletes emails it should not

I have recently been archiving user mailboxes and then using search-mailbox to delete emails before a certain date, dependant on guidance from the user.

The commands I have been using are

$date = (get-date).AddDays(-xxx).Date.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")

-xxx sets a date variable that is inline with how far back the user would like to keep their email.

search-mailbox -Identity <alias> -TargetMailbox <alias> -TargetFolder SearchandDelete -SearchQuery {(Receieved -lt $date)} -LogOnly -LogLevel full

This line will do a logonly and send a report to my mailbox with the results. Usually this returns more than 10,000 results so the following like is ran a few times to purge everything before the variable date.

search-mailbox -Identity <alias> -SearchQuery {(Receieved -lt $date)} -DeleteContent

However, using this method also seems to delete some current emails! Let's say the user wishes to keep the last 365 days of email. It is still actually removing emails from the users inbox that may only date back a few days.

Is this a known issue? Are there any workarounds or should I alter my powershell commandlets in any way?


  • Edited by cplg Monday, April 20, 2015 1:56 PM edited for clarity
April 20th, 2015 12:40pm

So I recall there were a few issues with date formats that could cause this in 2010. i.e. you needed to use dd-mm-yyyy instead of yyy-mm-dd.  Here is a link talking about a similar issue with New-MailboxExportRequest that may be of assistance in resolution https://social.technet.microsoft.com/Forums/exchange/en-US/74e7601c-50d2-4b79-8d5e-5d2f362252f8/exchange-2010-sp1-script-newmailboxexportrequest-with-contentfilter-parameters?forum=exchange2010
Free Windows Admin Tool Kit Click here and download it now
April 22nd, 2015 12:18pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics