Avatar of whatEVOwrx
whatEVOwrx
 asked on

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

Powershell

Avatar of undefined
Last Comment
Jeremy Weisinger

8/22/2022 - Mon
Michael B. Smith

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).
whatEVOwrx

ASKER
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. Smith

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
whatEVOwrx

ASKER
Okay, let me try that. Thanks!
ASKER CERTIFIED SOLUTION
Jeremy Weisinger

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
whatEVOwrx

ASKER
Thanks for the suggestion Jeremy!  I eventually got this to work the modified code with one modification.  I changed this [int] to [uint64].
Jeremy Weisinger

Ah yes, the byte count could get rather large. Wasn’t thinking about that. Glad to help. :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.