Is it possible to sort the output of the Format-List cmdlet by property name?
Suppose that I have an object $x with two properties \"A\" and \"B\", and when I run Format
By using Select-Object
with a calculated property (@{}
) and then excluding it (-ExcludeProperty
) you can also order the properties as you want. This works even when you don't know what's coming upfront.
@(
[PSCustomObject]@{
Color = 'Green'
Type = 'Fruit'
Name = 'kiwi'
Flavour = 'Sweet'
}
) | Select-Object @{Name = 'Flavour'; Expression = { $_.Flavour } },
@{Name = 'Name'; Expression = { $_.Name } }, * -ExcludeProperty Name, Flavour |
Format-List
Output:
Flavour : Sweet
Name : kiwi
Color : Green
Type : Fruit