Our thinking

Sort Office 365 Exchange Online mailbox folders by size in PowerShell

PowerShell is great, except when it isn’t. One of the really good things about it is that it can pipe objects between commands, instead of plain text like a lot of Unix commands do. This is all really good when you’re working on your local infrastructure, however it kind-of breaks when you’re working with Office 365.

If you connect to Exchange Online via the Connect-ExchangeOnline cmdlet in PowerShell, all of the values that get returned to your local cmdlets are converted from objects to XML and send to your local session.

What this means is that if you, for example, want to sort a list of folders by size in Exchange Online, it does not work.

The way it should work is very straightforward:

Get-EXOMailboxFolderStatistics -Identity [email protected] | Sort-Object FolderSize | ft FolderPath,FolderSize

What this should do is give me a list of all folders, showing the full folder path and sort them by size. What it actually does however is give me a list of all folders, showing the full folder path, and then sorts them by the string value that represents their size.

This means that the folder:

/Inbox/Travel 99.83 MB (104,677,250 bytes)

will show up as being bigger in the list over another folder:

/Inbox 8.545 GB (9,175,091,118 bytes)

Because 9 is bigger than 8. Not very helpful.

Fortunately help is at hand with some inline string processing.

Get-EXOMailboxFolderStatistics -Identity [email protected] | `
sort-object @{ Expression = {$tmp = $_.FolderSize -replace ".*\((.+) bytes\)","`$1"; [uint64]$foldersize = $tmp -replace ",",""; $foldersize }; Ascending=$false } | `
ft FolderPath,FolderSize

What this does is replace the folder size in human-readable units with the size in bytes, and then sorts on that.

This example came from here:

Leave a Reply