I’m trying to create a script that will extract users mailbox sizes and export to CSV, I have a script thanks to a post I found on Spiceworks (credit to @risingflight ) and I’ve got it to do what I want, but I’m struggling to get it to sort by mailbox size.

I tried adding an expression to the end of MailboxUsage = $Stats.TotalItemSize to convert the value to MB but it get’s ignored, likewise I’ve tried using MailboxUsage = $Stats.TotalItemSize.value.ToMB which is also seemingly ignored.

Below is the code I currently have.

Foreach($users in Get-Mailbox -ResultSize Unlimited){$users | Foreach-Object {  
$user = $_
$stats = Get-MailboxStatistics $user.Name
New-Object -TypeName PSObject -Property @{      
DisplayName = $User.DisplayName
RetentionPolicy = $User.RetentionPolicy                
MailboxUsage = $Stats.TotalItemSize
ItemCount = $Stats.ItemCount
}
} | Select-Object Displayname,MailboxUsage,ItemCount,RetentionPolicy | 
Export-CSV "C:\somepath\Mailbox_Size.csv" -NoTypeInformation -Append
}

Below is the code I was using, essentially I now want to include other values such as retention policy.

Get-Mailbox -ResultSize Unlimited |
Get-MailboxStatistics |
Select DisplayName, `
@{name="TotalItemSize (MB)"; expression={[math]::Round( `
($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}}, `
ItemCount |
Sort "TotalItemSize (MB)" -Descending |
Export-CSV "C:\somepath\Mailbox_Size.csv"
4 Spice ups

Check this site out Export Office 365 Mailbox Size Report Using PowerShell  and maybe this report too Export Office 365 Archive Mailbox Size Using PowerShell they have so many reports you can comb through and a mountain of information.

1 Spice up

You can also use the following script; it exports the data to a csv file named mailboxstas.csv on the Desktop.

$Mailboxes = Get-Mailbox | ? {$_.RecipientTypeDetails -eq “UserMailbox”}

$Result1 = @()

foreach ($Mailboxes in $Mailboxes) {

$a = Get-MailboxStatistics $Mailboxes.Alias | select TotalItemSize,Displayname

$b = Get-MailboxFolderStatistics $Mailboxes.Alias | ? {$.Name -eq “Recoverable Items”} | select @{Expression={};Label=“Name”;}, @{Expression={};Label=“Mailboxsize”;}, @{Expression={$.FolderAndSubfolderSize};Label=“RecoverableItemsSize”;}

$b.Mailboxsize= $a.TotalItemSize

$b.Name = $a.Displayname

$Result1 += $b

}

$Result1 | Export-Csv C:\Users\Administrator\Desktop\mailboxstas.csv -NoTypeInformation

I hope this will help.

Also check this thread for help - Export Users mailbox size from exchange online into csv file? | Microsoft Learn

1 Spice up

Thanks for the replies, https://o365reports.com looks interesting will have to spend sometime looking through it.