Write-Host "Gathering Stats, Please Wait.."

$Mailboxes = Get - Mailbox - ResultSize Unlimited | Select UserPrincipalName , identity , ArchiveStatus

$MailboxSizes = @ ( )

foreach ( $Mailbox in $Mailboxes ) {

$ObjProperties = New-Object PSObject

$MailboxStats = Get - MailboxStatistics $Mailbox .UserPrincipalname | Select LastLogonTime , @ { name = "TotalItemSizeGB" ;expression = { [ math ] ::Round ( ( ( $_ .TotalItemSize.Value.ToString ( ) ) .Split ( "(" ) [ 1 ] .Split ( " " ) [ 0 ] .Replace ( "," , "" ) / 1GB ) , 2 ) } } , ItemCount

$MailboxPurgeFolderStats = Get - MailboxFolderStatistics $Mailbox .UserPrincipalName | where-object { $_ .name -like "Purges" } | Select Name ,@ { name = "FolderSizeGB" ;E = { [ math ] ::round ( ( [ decimal ] ( ( $_ .FolderSize -replace "[0-9\.]+ [A-Z]* \(([0-9,]+) bytes\)" , " `$ 1" ) -replace "," , "" ) / 1GB ) , 2 ) } } , itemsinfolder

$MailboxDiscoveryHoldsFolderStats = Get - MailboxFolderStatistics $Mailbox .UserPrincipalName | where-object { $_ .name -like "DiscoveryHolds" } | Select Name ,@ { name = "FolderSizeGB" ;E = { [ math ] ::round ( ( [ decimal ] ( ( $_ .FolderSize -replace "[0-9\.]+ [A-Z]* \(([0-9,]+) bytes\)" , " `$ 1" ) -replace "," , "" ) / 1GB ) , 2 ) } } , itemsinfolder

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "UserPrincipalName" -Value $Mailbox .UserPrincipalName

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Last Logged In" -Value $MailboxStats .LastLogonTime

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Size" -Value $MailboxStats .TotalItemSizeGB

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Mailbox Item Count" -Value $MailboxStats .ItemCount

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Purges Folder Size" -Value $MailboxPurgeFolderStats .FolderSizeGB

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Purge Items" -Value $MailboxPurgeFolderStats .itemsinfolder

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Discovery Holds Folder Size" -Value $MailboxDiscoveryHoldsFolderStats .FolderSizeGB

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Discovery Hold Items" -Value $MailboxDiscoveryHoldsFolderStats .itemsinfolder

if ( $Mailbox .ArchiveStatus -eq "Active" ) {

$ArchiveStats = Get - MailboxStatistics $Mailbox .UserPrincipalname - Archive | Select @ { name = "TotalItemSizeGB" ;expression = { [ math ] ::Round ( ( ( $_ .TotalItemSize.Value.ToString ( ) ) .Split ( "(" ) [ 1 ] .Split ( " " ) [ 0 ] .Replace ( "," , "" ) / 1GB ) , 2 ) } } , ItemCount

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value $ArchiveStats .TotalItemSizeGB

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value $ArchiveStats .ItemCount

}

else {

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Size" -Value "No Archive"

Add-Member -InputObject $ObjProperties -MemberType NoteProperty -Name "Archive Item Count" -Value "No Archive"

}

$MailboxSizes += $ObjProperties

}