Sorting PS output is not working properly.

Hello All,

I am using below code to display the ClusterName, CSV name, Freespace and Percentage free and trying to sort it with Freespace but the output is not sorting it correctly due to comma separated values.

 
[pre]$data = @()

$VMMCluster = Get-scvmhostcluster

foreach ($cluster in $VMMCluster)

{

$data += $cluster.SharedVolumes.GetEnumerator() | Select @{l=‘ClusterName’;e={$cluster.name}},

@{l=‘ClusterStorage’;e={$_.name}},

@{l=‘Freespace’;e={"{0:N2}"-f ($_.freespace/1gb )}},

@{l=‘percentage’;e={"{0:N2}" -f ($.freespace / ($.Capacity/100))}}

}

$data | Sort-Object -Descending -Property freespace

 

Output:

ClusterStorage Freespace percentage


C:\ClusterStorage\Volume34 983.90 48.05
C:\ClusterStorage\Volume12 966.17 47.18
C:\ClusterStorage\Volume40 965.47 23.57
C:\ClusterStorage\Volume45 951.24 46.45
C:\ClusterStorage\Volume49 925.16 22.59
C:\ClusterStorage\Volume48 902.50 22.03
C:\ClusterStorage\Volume29 885.05 43.22
C:\ClusterStorage\Volume31 883.87 43.16
C:\ClusterStorage\Volume2 875.79 42.77
C:\ClusterStorage\Volume9 874.10 42.68
C:\ClusterStorage\Volume30 871.31 42.55
C:\ClusterStorage\Volume55 869.14 21.22
C:\ClusterStorage\Volume23 869.08 42.44
C:\ClusterStorage\Volume5 862.89 42.14
C:\ClusterStorage\Volume6 862.01 21.05
C:\ClusterStorage\Volume26 854.22 41.71
C:\ClusterStorage\Volume32 837.53 40.90
C:\ClusterStorage\Volume48 834.48 20.37
C:\ClusterStorage\Volume39 829.43 20.25
C:\ClusterStorage\Volume21 828.62 40.46
C:\ClusterStorage\Volume46 809.96 19.77
C:\ClusterStorage\Volume47 807.74 39.44
C:\ClusterStorage\Volume15 805.66 39.34
C:\ClusterStorage\Volume20 795.10 38.83
C:\ClusterStorage\Volume16 790.16 19.29
C:\ClusterStorage\Volume9 775.36 37.86
C:\ClusterStorage\Volume22 753.54 36.80
C:\ClusterStorage\Volume18 748.08 18.26
C:\ClusterStorage\Volume45 738.88 18.04
C:\ClusterStorage\Volume42 734.85 35.88
C:\ClusterStorage\Volume50 724.26 17.68
C:\ClusterStorage\Volume9 721.84 35.25
C:\ClusterStorage\Volume3 719.17 35.12
C:\ClusterStorage\Volume43 717.81 35.05
C:\ClusterStorage\Volume33 717.36 35.03
C:\ClusterStorage\Volume28 710.97 34.72
C:\ClusterStorage\Volume5 694.08 33.89
C:\ClusterStorage\Volume37 683.00 33.35
C:\ClusterStorage\Volume22 665.76 32.51
C:\ClusterStorage\Volume21 662.78 32.36
C:\ClusterStorage\Volume41 652.29 31.85
C:\ClusterStorage\Volume1 64.72 3.16
C:\ClusterStorage\Volume14 638.88 31.20
C:\ClusterStorage\Volume34 627.15 30.62
C:\ClusterStorage\Volume46 625.39 30.54
C:\ClusterStorage\Volume10 621.78 30.36
C:\ClusterStorage\Volume10 618.27 30.19
C:\ClusterStorage\Volume16 598.26 14.61
C:\ClusterStorage\Volume15 596.13 14.55
C:\ClusterStorage\Volume7 591.25 28.87
C:\ClusterStorage\Volume17 590.04 14.41
C:\ClusterStorage\Volume51 583.01 28.47
C:\ClusterStorage\Volume15 577.74 28.21
C:\ClusterStorage\Volume7 572.96 27.98
C:\ClusterStorage\Volume3 569.84 27.83
C:\ClusterStorage\Volume1 564.46 27.56
C:\ClusterStorage\Volume17 562.10 13.72
C:\ClusterStorage\Volume24 560.62 27.38
C:\ClusterStorage\Volume43 556.26 13.58
C:\ClusterStorage\Volume4 547.83 26.75
C:\ClusterStorage\Volume5 547.78 26.75
C:\ClusterStorage\Volume41 543.49 26.54
C:\ClusterStorage\Volume2 542.67 26.50
C:\ClusterStorage\Volume25 542.63 26.50
C:\ClusterStorage\Volume47 537.62 13.13
C:\ClusterStorage\Volume19 536.26 26.19
C:\ClusterStorage\Volume30 531.88 25.97
C:\ClusterStorage\Volume14 526.49 12.85
C:\ClusterStorage\Volume51 525.53 25.66
C:\ClusterStorage\Volume8 518.97 25.34
C:\ClusterStorage\Volume28 515.31 25.16
C:\ClusterStorage\Volume13 514.33 25.12
C:\ClusterStorage\Volume8 513.33 25.07
C:\ClusterStorage\Volume31 498.32 24.33
C:\ClusterStorage\Volume18 493.42 24.09
C:\ClusterStorage\Volume49 486.77 11.88
C:\ClusterStorage\Volume17 459.43 11.22
C:\ClusterStorage\Volume2 456.57 22.29
C:\ClusterStorage\Volume10 455.75 22.26
C:\ClusterStorage\Volume24 452.24 22.08
C:\ClusterStorage\Volume16 449.99 10.99
C:\ClusterStorage\Volume42 443.25 21.64
C:\ClusterStorage\Volume20 435.58 10.63
C:\ClusterStorage\Volume12 432.62 21.13
C:\ClusterStorage\Volume3 432.08 21.10
C:\ClusterStorage\Volume30 429.70 20.98
C:\ClusterStorage\Volume8 418.75 20.45
C:\ClusterStorage\Volume13 416.89 20.36
C:\ClusterStorage\Volume14 413.61 10.10
C:\ClusterStorage\Volume6 406.87 19.87
C:\ClusterStorage\Volume15 404.43 9.87
C:\ClusterStorage\Volume26 403.17 19.69
C:\ClusterStorage\Volume1 398.90 19.48
C:\ClusterStorage\Volume1 397.42 19.41
C:\ClusterStorage\Volume52 395.58 19.32
C:\ClusterStorage\Volume11 394.99 19.29
C:\ClusterStorage\Volume50 388.74 18.98
C:\ClusterStorage\Volume5 387.35 18.91
C:\ClusterStorage\Volume12 385.77 18.84
C:\ClusterStorage\Volume12 385.55 18.83
C:\ClusterStorage\Volume36 384.79 18.79
C:\ClusterStorage\Volume4 380.41 18.57
C:\ClusterStorage\Volume9 379.86 18.55
C:\ClusterStorage\Volume35 377.46 18.43
C:\ClusterStorage\Volume38 372.12 18.17
C:\ClusterStorage\Volume39 371.25 18.13
C:\ClusterStorage\Volume50 370.92 18.11
C:\ClusterStorage\Volume49 366.25 17.88
C:\ClusterStorage\Volume23 361.26 17.64
C:\ClusterStorage\Volume29 359.78 17.57
C:\ClusterStorage\Volume25 358.89 8.76
C:\ClusterStorage\Volume42 355.78 17.37
C:\ClusterStorage\Volume27 346.28 16.91
C:\ClusterStorage\Volume2 345.34 16.86
C:\ClusterStorage\Volume11 344.92 16.84
C:\ClusterStorage\Volume10 343.38 16.77
C:\ClusterStorage\Volume4 332.36 16.23
C:\ClusterStorage\Volume26 311.42 15.21
C:\ClusterStorage\Volume6 304.78 14.88
C:\ClusterStorage\Volume14 304.41 14.86
C:\ClusterStorage\Volume4 3,994.72 97.53
C:\ClusterStorage\Volume15 3,946.32 96.35
C:\ClusterStorage\Volume17 3,933.56 96.04
C:\ClusterStorage\Volume8 3,122.95 76.25
C:\ClusterStorage\Volume9 3,120.97 76.20
C:\ClusterStorage\Volume7 295.12 14.41
C:\ClusterStorage\Volume35 286.05 13.97
C:\ClusterStorage\Volume40 269.20 13.15
C:\ClusterStorage\Volume3 267.50 13.06
C:\ClusterStorage\Volume13 256.26 6.26
C:\ClusterStorage\Volume11 238.60 11.65
C:\ClusterStorage\Volume27 236.55 11.55
C:\ClusterStorage\Volume6 219.01 10.69
C:\ClusterStorage\Volume18 2,926.60 71.45
C:\ClusterStorage\Volume7 2,833.18 69.17
C:\ClusterStorage\Volume10 2,788.77 68.09
C:\ClusterStorage\Volume14 2,751.10 67.17
C:\ClusterStorage\Volume11 2,441.74 59.61
C:\ClusterStorage\Volume9 2,114.90 34.42
C:\ClusterStorage\Volume5 2,047.57 99.99
C:\ClusterStorage\Volume40 2,047.57 99.99
C:\ClusterStorage\Volume19 2,047.57 99.99
C:\ClusterStorage\Volume28 2,047.57 99.99
C:\ClusterStorage\Volume1 2,047.57 99.99
C:\ClusterStorage\Volume38 127.54 6.23
C:\ClusterStorage\Volume22 1,975.04 96.44
C:\ClusterStorage\Volume17 1,953.47 95.39
C:\ClusterStorage\Volume27 1,920.25 93.77
C:\ClusterStorage\Volume34 1,901.61 92.86
C:\ClusterStorage\Volume19 1,807.39 88.26
C:\ClusterStorage\Volume36 1,719.57 41.98
C:\ClusterStorage\Volume4 1,659.89 40.53
C:\ClusterStorage\Volume32 1,607.84 78.51
C:\ClusterStorage\Volume56 1,537.97 37.55
C:\ClusterStorage\Volume18 1,464.00 35.74
C:\ClusterStorage\Volume11 1,345.07 32.84
C:\ClusterStorage\Volume20 1,281.13 20.85
C:\ClusterStorage\Volume37 1,242.62 60.68
C:\ClusterStorage\Volume57 1,156.93 28.25
C:\ClusterStorage\Volume7 1,144.97 55.91
C:\ClusterStorage\Volume5 1,131.90 27.64
C:\ClusterStorage\Volume44 1,128.75 27.56
C:\ClusterStorage\Volume13 1,097.81 53.61
C:\ClusterStorage\Volume32 1,004.04 24.51
[/pre]

I would like to correctly sort it using Freespace from lowest to highest? Any help would be appreciated.

It looks like FreeSpace is a string. You can either try storing the value as a number of some kind, or sort while casting:

[pre]$data | Sort-Object -Descending {[decimal]$_.FreeSpace}[/pre]

Edit: Didn’t spot decimals and commas. Sorry about that! Thanks for the spot, kvprasoon!

If converted to Int, will end up in ignoring the decimals. Try

@{l='Freespace';e={($_.freespace/1gb).ToString("N2").Replace(",","")}}