How to get a list of emails older than 2 years in Exchange Database, sorted by user, using Powershell?
Look into Multi-Mailbox Search/Discovery Search for what you need. You could get per-mailbox stats of individual folders by editing your existing script, but in order to get the size of all mail received within a date range reference the above link. It is not going to be a quick search by any means...
I was trying to solve this same problem and came up with the following.
You'll want to define $location
as well as change the addyears(-1)
to which ever number of years you want. In this example -1 is 1 year ago.
$Mailbox = Get-MailboxDatabase | Get-Mailbox
Foreach ($MBX in $Mailbox) {
$usermailbx = Get-Mailbox -identity $MBX | Get-MailboxStatistics
$userarchmailbx = Get-Mailbox -identity $MBX | search-mailbox -SearchQuery "received<=$((get-date).addyears(-1).toString("yyyy-MM-ddTHH:mm:ssZ"))" -EstimateResultOnly
[pscustomobject]@{UserName=$usermailbx.displayname;TotalItemCount=$usermailbx.ItemCount;TotalItemSize=$usermailbx.totalitemsize.value;DeletedItemSize=$usermailbx.totaldeleteditemsize.value;ArchiveSize=$userarchmailbx.ResultItemsSize} | export-csv -append "$location\file.csv"
}