Scenario: You want to delete mail items (not calendar or contact items) from all mailbox folders in a mailbox. The script performs a query for all email items between a date range.
Script:
$startdate = ’01/01/1900′ #specifies start date
$enddate = (get-date).adddays(-60) #specifies end date by subtracting 60 days
$enddate = $enddate.ToShortDateString() #converts end date to string.
$users = Import-csv C:scriptusers.csv #imports list of users with the column heading ‘name’
#Deletes email content between the two dates for each mailbox.
$users | ForEach {
search-mailbox $_.name -searchquery “kind:email AND Received:$startdate..$enddate” -deletecontent -force
}
Search-Mailbox has a 10,000 item limit that search-mailbox before it stops processing. Put it in a loop and let it run. The example below is for a single mailbox outside of the script above.
do {
Write-Host $i
search-mailbox mailboxname -searchquery “kind:email AND Received:1/1/1900..12/31/2012” -deletecontent -force
$i++
}
while ($i -le 30)
We’re migrating from an older Journal Mailbox DB to a newer one in Exchange 2013. Our older DB has millions of items, so we’re having issues and are resorting to this search-mailbox commandlet, but I think we’re hitting the 10,000 item limit you mention. Looking at the last portion of your script that deals with the 10K item limit issue, I’m having a hard time understanding how this gets around the issue. Could you explain in further detail?
LikeLike
Hey Ryan. Since the -searchquery is specified with the search-mailbox, it will only pull 10,000 results. When using the -DeleteContent, it will delete those 10,000 Items. By running this command again, it will pull the next 10,000 results and delete those 10,000 items as well. Thus far you have deleted 20,000 Items. Putting this command into a loop repeats this process. As an example, if you want to delete 50,000 Items from a mailbox, you will have to run this command 5 times. You can run it manually or putting into the loop and letting it run automatically.
LikeLike
Interesting. So with this loop specificaly, it knows to pick up where it left off at? I guess that does make sense in this case, as it’s deleting emails as it goes along, so when it runs the command to search again, it’s only searching for new content, as the previous content has already been deleted.We’re trying to copy the data to a new Journal DB, so I’ll be curious how it works. I’m doing a test right now for a few days to see how it works now. I know with mailbox import requests from PSTs, it won’t import duplicate emails, so hopefully the same holds true for this sort of import and search.I appreciate you getting back to me so promptly on this!
LikeLike