# sort-object not working

**URL:** <https://forums.powershell.org/t/sort-object-not-working/804>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/sort-object-not-working/804 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/sort-object-not-working/804/1 "2011-12-31T18:30:00Z")

</div>

by penguinviw at 2013-04-02 11:55:33

> I have this script, but it does not sort.  
> `Import-Module ActiveDirectoryif(!(Get-PSSnapin | Where-Object {$.name -eq "Microsoft.Exchange.Management.PowerShell.E2010"})) { ADD-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 }$ErrorActionPreference = "SilentlyContinue"$WarningPreference = "SilentlyContinue"$oulist = Get-OrganizationalUnit "KT" -SingleNodeOnlyForEach ($Ounames in $Oulist){	Write-Host "--------------------------"	Write-Host "$Ounames" Write-Host "--------------------------" $Results = @() ForEach ($Mailbox in (Get-Mailbox -ResultSize Unlimited -OrganizationalUnit $Ounames.Name)){ $Results += Get-mailboxstatistics $Mailbox | Sort-Object TotalItemSize -Descending | Select @{label="User";expression={$.DisplayName}}, <br> @{label=&quot;TotalSize&#40;MB&#41;&quot;;expression={$_.TotalItemSize.Value.ToMB&#40;&#41;}}, @{label="Items";expression={$.ItemCount}}, <br> @{label=&quot;Storage Limit&quot;;expression={$_.StorageLimitStatus}}, @{name="PrimarySMTPAddress";expression={$mailbox.PrimarySMTPAddress}}, ` Database,ServerName }	$Results | Where { $ -ne $null } | ft -Autosize}`  
> Thanks for any input!!

by coderaven at 2013-04-02 12:02:22

> Sort after you do your select-object by the attributes you create in that process. I would even recommend moving it after all of your other modifications like the where filter. The last thing you do would be the Sort-Object, otherwise the sort it lost.  
>   
> `$Results | Where-Object {…} | Sort-Object … | Format-Table …`

by penguinviw at 2013-04-02 13:17:32

> Thanks for the reply. I changed the code so that the sort-object was in the line you listed above, but it still does not work.  
> `$Results | Where { $_ -ne $null } | Sort-Object TotalItemSize -Descending | ft -Autosize`  
> Any ideas?  
> Thanks!!

by coderaven at 2013-04-02 13:32:03

> In your total select, you remove totalitemsize and replace it with TotalSize(MB). Try sorting by that.

by penguinviw at 2013-04-02 16:41:28

> The line now looks like:  
> `$Results | Where { $_ -ne $null } | Sort-Object TotalSize(MB) -Descending |ft -Autosize`  
> Now it only lists the OU names and nothing else. Very wierd.

by cmcknz77 at 2013-04-02 16:48:03

> What do you get if you pipe $Results to Get-Member? i.e.:-  
> [code2=powershell]$Results | Where { $\_ -ne $null } | Get-Member[/code2]

by cmcknz77 at 2013-04-02 16:59:08

> or   
> [code2=powershell]$Results | Get-Member[/code2]  
>   
> Additionally it might be worth taking the Brackets out of your ‘TotalSize(MB)’ label on your custom objects… Powershell tends to try to evaluate anything in parenthesis before the rest of the code so it might be worth you doing this instead:  
>   
> [code2=powershell]$Results += Get-mailboxstatistics $Mailbox |   
> Select @{label="User";expression={$_.DisplayName}},  
> @{label="TotalSize";expression={$_.TotalItemSize.Value.ToMB()}},  
> @{label="Items";expression={$_.ItemCount}},   
> @{label="Storage Limit";expression={$_.StorageLimitStatus}},  
> @{name="PrimarySMTPAddress";expression={$mailbox.PrimarySMTPAddress}},  
> Database,  
> ServerName}  
>   
> $Results | Where { $\_ -ne $null } | Sort-Object TotalSize -Descending |ft -Autosize[/code2]  
>   
> or enclosing the ‘TotalSize(MB)’ property like so:  
> [code2=powershell]$Results | Where { $\_ -ne $null } | Sort-Object {TotalSize(MB)} -Descending |ft -Autosize[/code2]

by penguinviw at 2013-04-02 17:31:24

> Thanks so much !!!  
> I followed your suggestion and changed "TotalSize(MB)" to "TotalSizeMB". That fixed it.  
> AWESOME!!! I been trying all kinds of things.

by cmcknz77 at 2013-04-03 00:10:21

> Awesome… Happy to help… So this is solved?

by MasterOfTheHat at 2013-04-03 07:10:52

> [quote="penguinviw"]The line now looks like:  
> `$Results | Where { $_ -ne $null } | Sort-Object TotalSize(MB) -Descending |ft -Autosize`  
> Now it only lists the OU names and nothing else. Very wierd.[/quote]  
> Wrap the column name in double quotes. That will tell posh to treat it as a string literal instead of an expression.  
> `$Results | Where { $_ -ne $null } | Sort-Object "TotalSize(MB)" -Descending |ft -Autosize`  
> Similar example:  
> `Get-WmiObject win32_logicaldisk | Select-Object DeviceID, @{label="Free Space (MB)";expression={$_.FreeSpace / 1MB}} | Sort-Object "Free Space (MB)" | ft -AutoSize`  
> Output:  
> `DeviceID Free Space (MB)-------- ---------------G: 0E: 0D: 1686.484375C: 70629.08984375F: 793330.8359375`

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:58pm UTC](https://forums.powershell.org/t/sort-object-not-working/804/2 "2024-05-16T20:58:02Z")

</div>


