Sorting Recoverable Items Size in Descending Order

Hello,

I have a script modified from various other scripts to match what I need.  Check the attached code.  I'm trying to Sort Recoverable Items Size by size in Descending order.  However I'm not getting results from big to small.

The below script works fine if I sort "Total Item Size (MB)".

$mailboxes = @(Get-Mailbox -Filter "RecipientType -eq 'UserMailbox'" -ResultSize 20)
$report = @()

ForEach ($mailbox in $mailboxes)
{
    $mbFolderStats = $mailbox | Get-MailboxFolderStatistics | where {$_.FolderType -eq ‘RecoverableItemsRoot’}
    $mbStats = $mailbox | Get-MailboxStatistics | Select-Object DisplayName, @{name="TotalItemSize (MB)";expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}},ItemCount,TotalDeletedItemSize
    #Select-Object DisplayName, @{name=”TotalItemSize (GB)”;expression={[math]::Round((($_.TotalItemSize.Value.ToString()).Split(“(“)[1].Split(” “)[0].Replace(“,”,””)/1GB),2)}},ItemCount,TotalDeletedItemSize -First 50

    $mbObj = New-Object PSObject
    $mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $mailbox.DisplayName
    $mbObj | Add-Member -MemberType NoteProperty -Name "Folder Path" -Value $mbFolderStats.Name
    $mbObj | Add-Member -MemberType NoteProperty -Name "Recoverable Items Size" -Value $mbFolderStats.FolderAndSubfolderSize
    $mbobj | Add-Member -MemberType NoteProperty -Name "Items in Folder" -Value $mbFolderStats.ItemsInFolderAndSubfolders
    $mbObj | Add-Member -MemberType NoteProperty -Name "Total Item Size (MB)" -Value $mbStats.'TotalItemSize (MB)'
    $mbObj | Add-Member -MemberType NoteProperty -Name "Item Count" -Value $mbStats.ItemCount
    $mbObj | Add-Member -MemberType NoteProperty -Name "Total Deleted Item Size" -Value $mbStats.TotalDeletedItemSize
    $report += $mbObj
    }
$report | Sort-Object "Recoverable Items Size" -Descending

Open in new window

whatEVOwrxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael B. SmithManaging ConsultantCommented:
What Exchange version? What PowerShell version?

It works just fine for me. I tested Exchange 2016 on Windows Server 2012 R2 (PowerShell 4.0) and Exchange 2019 on Windows Server 2019 (PowerShell 5.1).
whatEVOwrxAuthor Commented:
Powershell version 5.1 and I'm connecting to Office365.  Hmmm, check below for a Sort compared to one that's not.  The 1st image is Sorted and the 2nd is unsorted.
Sort.pngUnsorted.png
Michael B. SmithManaging ConsultantCommented:
Notice that the first numeric digits are, indeed, sorted.

This is happening because Office 365 is returning those values as _strings_ instead of as numbers.

Look at $mbFolderStats.FolderAndSubfolderSize (gm -i $mbFolderStats.FolderAndSubfolderSize) and see if you can get all values in the same value range (e.g., ToGB(), ToMB(), ToByte() ). If you can, then the sort will then work.
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

whatEVOwrxAuthor Commented:
Okay, let me try that. Thanks!
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
I added a property that converted the string to bytes, sorted by the bytes and then excluded the property. See if that works for you:

$mailboxes = @(Get-Mailbox -Filter "RecipientType -eq 'UserMailbox'" -ResultSize 20)
$report = @()

ForEach ($mailbox in $mailboxes)
{
    $mbFolderStats = $mailbox | Get-MailboxFolderStatistics | where {$_.FolderType -eq ‘RecoverableItemsRoot’}
    $mbStats = $mailbox | Get-MailboxStatistics | Select-Object DisplayName, @{name="TotalItemSize (MB)";expression={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}},ItemCount,TotalDeletedItemSize
    #Select-Object DisplayName, @{name=”TotalItemSize (GB)”;expression={[math]::Round((($_.TotalItemSize.Value.ToString()).Split(“(“)[1].Split(” “)[0].Replace(“,”,””)/1GB),2)}},ItemCount,TotalDeletedItemSize -First 50

    $mbObj = New-Object PSObject
    $mbObj | Add-Member -MemberType NoteProperty -Name "Display Name" -Value $mailbox.DisplayName
    $mbObj | Add-Member -MemberType NoteProperty -Name "Folder Path" -Value $mbFolderStats.Name
    $mbObj | Add-Member -MemberType NoteProperty -Name "Recoverable Items Size" -Value $mbFolderStats.FolderAndSubfolderSize
    $mbObj | Add-Member -MemberType NoteProperty -Name "RecoverableItemSizeBytes" -Value ([int]$mbFolderStats.FolderAndSubfolderSize.Split('(')[1].TrimEnd(" bytes)"))
    $mbobj | Add-Member -MemberType NoteProperty -Name "Items in Folder" -Value $mbFolderStats.ItemsInFolderAndSubfolders
    $mbObj | Add-Member -MemberType NoteProperty -Name "Total Item Size (MB)" -Value $mbStats.'TotalItemSize (MB)'
    $mbObj | Add-Member -MemberType NoteProperty -Name "Item Count" -Value $mbStats.ItemCount
    $mbObj | Add-Member -MemberType NoteProperty -Name "Total Deleted Item Size" -Value $mbStats.TotalDeletedItemSize
    $report += $mbObj
    }
$report | Sort-Object "RecoverableItemSizeBytes" -Descending | Select-Object * -ExcludeProperty RecoverableItemSizeBytes

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
whatEVOwrxAuthor Commented:
Thanks for the suggestion Jeremy!  I eventually got this to work the modified code with one modification.  I changed this [int] to [uint64].
Jeremy WeisingerSenior Network Consultant / EngineerCommented:
Ah yes, the byte count could get rather large. Wasn’t thinking about that. Glad to help. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.