VMware Cloud Community
ehsanijavad
Enthusiast
Enthusiast
Jump to solution

Sort : The -f Format operator

Hi all

could someone help, how can sort The -f Format operators.

https://ss64.com/ps/syntax-f-operator.html

for example:


$VMs = Get-Cluster xxxxxx | Get-VM

$Rep = @()

$VMs | foreach {

$Rep += New-Object psobject -Property @{

VM = $_.Name

ProvSpaceGB = &{"{0:n2}" -f ($_.ProvisionedSpaceGB,0)}
}
}

$Rep | sort ProvSpaceGB (it does not work)

 

Thanks in advance for your help.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Your ProvSpaceGB is a String, not a Float, after the -f operator.
So for example, the value 123 will come before 4.

If you want to sort on the numerical value, you can cast the property on which to sort to a Float.

$Rep | Sort-Object -Property {[float]$_.ProvSpaceGB}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Your ProvSpaceGB is a String, not a Float, after the -f operator.
So for example, the value 123 will come before 4.

If you want to sort on the numerical value, you can cast the property on which to sort to a Float.

$Rep | Sort-Object -Property {[float]$_.ProvSpaceGB}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ehsanijavad
Enthusiast
Enthusiast
Jump to solution

Hi LucD

Thanks for your answer.

0 Kudos