Script to export a list of all the mailboxes in an organisation and send the results as attachments to an email.
Gives a list ordered by size and after that ordered by last logon time (to hilight old inactive mailboxes).
Also creates and attaches a CSV listing the mailboxes and the size of each one.
###Send mailbox statistics script ### ### to schedule this use PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1" -Command "./scriptname.ps1" ### ### Change the mail message values in this section # this needs to be changed depending on version of exchange Add-PSSnapin "Microsoft.Exchange.Management.PowerShell.Admin" $FromAddress = "nanotera@nanotera.com.au" $ToAddress = "nanotera@nanotera.com.au" $MessageSubject = "Mailbox Size Report from $env:computername " $MessageBody = "Attached is the current list of mailbox sizes." ### put your SMTP server here $SendingServer = "192.168.1.1" $tempfilename= "$env:temp\listmailboxes1.txt" $tempfilenamemk1= "$env:temp\listmailboxes1_" Get-MailboxDatabase | sort-object "server","storagegroup" | Select Server, StorageGroupName, Name, @{Name="Size (GB)";Expression={$objitem = (Get-MailboxDatabase $_.Identity); $path = "`\`\" + $objitem.server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2); $size = ((Get-ChildItem $path).length)/1048576KB; [math]::round($size, 2)}}, @{Name="Size (MB)";Expression={$objitem = (Get-MailboxDatabase $_.Identity ) ; $path = "`\`\" + $objitem.server + "`\" + $objItem.EdbFilePath.DriveName.Remove(1).ToString() + "$"+ $objItem.EdbFilePath.PathName.Remove(0,2); $size = ((Get-ChildItem $path).length)/1024KB; [math]::round($size, 2)}}, @{Name="No. Of Mbx";expression={(Get-Mailbox -Database $_.Identity -resultsize unlimited | Measure-Object).Count}} | Format-table -AutoSize | out-file -width 255 $tempfilename foreach ($mbdatabase in Get-MailboxDatabase | sort-object "server","storagegroup" ) { echo "Mailboxes in $mbdatabase.identity sorted by size" | out-file -append -width 255 $tempfilename ###Now get the stats and store in a text file Get-MailboxStatistics -database $mbdatabase.identity | Sort-Object @{Expression="Database";Descending=$false} ,@{Expression="TotalItemSize";Descending=$true} | ft database, DisplayName, lastlogontime ,@{label="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.tomb()}}, ItemCount | out-file -append -width 255 $tempfilename echo "Mailboxes in $mbdatabase.identity sorted by last logon (oldest first)" | out-file -append -width 255 $tempfilename Get-MailboxStatistics -database $mbdatabase.identity | Sort-Object @{Expression="Database";Descending=$false} ,@{Expression="Lastlogontime";Descending=$false} | ft database, DisplayName, lastlogontime ,@{label="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.tomb()}}, ItemCount | out-file -append -width 255 $tempfilename } ###Create the mail message and add the statistics text file as an attachment $SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $ToAddress, $MessageSubject, $MessageBody $Attachment = New-Object Net.Mail.Attachment($tempfilename) $SMTPMessage.Attachments.Add($Attachment) # get them all in one CSV file per server foreach ( $mbserver in get-mailboxserver ) { $mbs = Get-MailboxStatistics -server $mbserver $mbs1 = $mbs | Sort-Object @{Expression="Database";Descending=$false} ,@{Expression="TotalItemSize";Descending=$true} $tmpfile1=$tempfilenamemk1 + $mbserver + ".csv" $mbs1 | select database, DisplayName, lastlogontime ,@{label="TotalItemSize(MB)";expression={$_.TotalItemSize.Value.tomb()}}, ItemCount | export-csv -notypeinformation -Path $tmpfile1 $Attachment = New-Object Net.Mail.Attachment($tmpfile1 ) $SMTPMessage.Attachments.Add($Attachment) } ###Send the message $SMTPClient = New-Object System.Net.Mail.SMTPClient $SendingServer $SMTPClient.Send($SMTPMessage) remove-variable SMTPclient remove-variable SMTPmessage Remove-Variable attachment #should remove temp files #remove-item "$tempfilename"