Link to home
Start Free TrialLog in
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

Avatar of Michael B. Smith
Michael B. Smith
Flag of United States of America image

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).
Avatar of whatEVOwrx
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.
User generated imageUser generated image
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.
Okay, let me try that. Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Jeremy Weisinger
Jeremy Weisinger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the suggestion Jeremy!  I eventually got this to work the modified code with one modification.  I changed this [int] to [uint64].
Ah yes, the byte count could get rather large. Wasn’t thinking about that. Glad to help. :)