Share via

Exchange PowerShell Script: Calculate mailbox size for last 30 days

MichaelFuller-1588 66 Reputation points
2022-03-31T14:45:16.057+00:00

I am working on a script that calculates the size of the last 30 days of email for an Exchange 2016 mailbox. I need to get the exact amount. The only way I know to do this is with mailbox-search. I have seen the get-mailboxfolderstatistics script that estimates the size of the last 30 days. That is not sufficient for my needs. The problem that I am having is that search-mailbox will not let me put resultItemSize into a variable. What am I doing wrong? Can this be done another way? Here is what I have.

$server=read-host "Enter server name"
$targetMailbox=read-host "Enter an email address on this network where the search mailbox folders will be created"

$startDate=(get-date).date.ToShortDateString()
$stopDate=(get-date).date.AddDays(-30).ToShortDateString()
$Report=@()
$mailboxes = Get-mailbox -server $server

foreach ($mailbox in $mailboxes) {
write-host "$($mailbox.primarysmtpaddress)"

Pulling Data

$last30daysinMB=((Search-Mailbox -Identity $mailbox -SearchQuery "received:$($startDate)..$($stopDate) OR sent:$($startDate)..$($stopDate)"  -TargetMailbox $targetMailbox -TargetFolder "Mailbox Size Script" -estimateResultsOnly).resultItemSize)


$TotalSizeInMB = ($(Get-MailboxStatistics -Identity $Mailbox | Select-Object -ExpandProperty TotalItemSize | Select-Object -ExpandProperty Value) -split '\(| ')[-2].Replace(',','')/1MB -as [int]

#Building report

$ReportObj = New-Object PSobject
Add-Member -InputObject $ReportObj -type NoteProperty -name User -value $mailbox.PrimarySMTPAddress
Add-Member -InputObject $ReportObj -type NoteProperty -name TotalMB -value $TotalSizeInMB
Add-Member -InputObject $ReportObj -type NoteProperty -name Last30DaysMB -value $last30daysinMB

$Report += $ReportObj

}

$Report | Export-Csv -NoTypeInformation -path "C:\Users\$env:username\Desktop\30DaysOfEmailCalculated-$(Get-Date -Format 'MM-dd-yyyy').csv" -force

Exchange | Exchange Server | Development
Exchange | Exchange Server | Management
Exchange | Exchange Server | Management

The administration and maintenance of Microsoft Exchange Server to ensure secure, reliable, and efficient email and collaboration services across an organization.

Windows for business | Windows Server | User experience | PowerShell

Answer accepted by question author
  1. Joyce Shen - MSFT 16,706 Reputation points
    2022-04-05T06:49:16.793+00:00

    Hi @MichaelFuller-1588

    I tried this in my environment, you could take a look:

    search-mailbox -Identity user -SearchQuery 'received>03/01/2022 OR sent>03/01/2022' -EstimateResultOnly  
    

    189958-image.png


    If an Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. MichaelFuller-1588 66 Reputation points
    2022-04-06T17:31:43.487+00:00

    Final Script

    $server=read-host "Enter server name"

    $stopDate=(get-date).date.AddDays(-30).ToShortDateString()
    $Report=@()
    $mailboxes = Get-mailbox -server $server

    foreach ($mailbox in $mailboxes) {
    write-host "$($mailbox.primarysmtpaddress)"

    #Pulling Data
    
    $last30days=((Search-Mailbox -Identity $mailbox -SearchQuery "received>$($stopDate) OR sent>$($stopDate)" -estimateResultOnly)
    
    $TotalSize = ($(Get-MailboxStatistics -Identity $Mailbox | Select-Object -ExpandProperty TotalItemSize | Select-Object -ExpandProperty Value) -split '\(| ')[-2].Replace(',','')/1MB -as [int]
    
    #Building report
    $ReportObj = New-Object PSobject
    Add-Member -InputObject $ReportObj -type NoteProperty -name User -value $mailbox.PrimarySMTPAddress
    Add-Member -InputObject $ReportObj -type NoteProperty -name TotalMB -value $TotalSize
    Add-Member -InputObject $ReportObj -type NoteProperty -name Last30DaysMB -value $last30days.ResultItemsSize
    Add-Member -InputObject $ReportObj -type NoteProperty -name Last30DaysItems -value $last30days.ResultItemsSize
    
    $Report += $ReportObj
    
    }
    

    $Report | Export-Csv -NoTypeInformation -path "C:\Users\$env:username\Desktop\30DaysOfEmailCalculated-$(Get-Date -Format 'MM-dd-yyyy').csv" -force

    0 comments No comments

  2. Rich Matheisen 48,116 Reputation points
    2022-03-31T18:42:25.47+00:00

    This is really an Exchange question.

    That said, the Search-Mailbox cmdlet, using the parameter set you're using should return an estimate of the number AND size of the messages. The fact that it's returning more than one result implies that the result would be something like a PSCustomObject, not a single value. But if you look at the output type for that cmdlet, it's "Mailbox". That seems to imply that the results you're looking for are going to be found in the mailbox identified by $targetmailbox in a folder in that mailbox named "Mailbox Size Script".

    Reading further, the description for the cmdlet says about the "-EstimateOnly" switch: "You can't use this switch with the TargetMailbox parameter.

    Maybe one pf the Exchange folks might be able to clarify, but I don't think you're going to be able to use the set of parameters you've chosen.

    Try dropping the targetmailbox and targeffolder parameters and see if that works any better.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.