格式列表:按名称对属性进行排序 [英] Format-List: sort properties by name
问题描述
是否可以通过属性名称对Format-List cmdlet的输出进行排序?
假设我有一个带有两个属性"A"和"B"的对象$ x,当我用它运行Format-List时,我得到了
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-List with it I get
(PS) > $x | Format-List
B : value b
A : value a
我想拥有
(PS) > $x | Format-List
A : value a
B : value b
注意:我应该从一开始就指定,与具有"A"和"B"属性的示例不同,我要处理的真实对象具有很多属性,并且将来可能会添加新的,所以我不知道所有的属性名称.
NOTE: I should have specified from the beginning that, unlike in the example with "A" and "B" properties, the real object I have to deal with has quite a lot of properties, and new ones could be added in the future, so I don't know all the property names in advance.
推荐答案
AFAIK,Format-List
不提供此类选项.
AFAIK, Format-List
does not provide such an option.
对于您的特定示例,这应该可行:
For your particular example this should work:
$x | Select-Object A, B | Format-List
如果属性集不固定/未知,则使用Get-Member
和某些预处理为Select-Object
排序参数数组的过程将更加棘手.
If the property set is not fixed/known then the procedure will be more tricky with use of Get-Member
and some preprocessing making sorted parameter array for Select-Object
.
在这里(让我们用$ host代替$ x):
Here it is (let's use $host instead of $x):
$host | Select-Object ([string[]]($host | Get-Member -MemberType Property | %{ $_.Name } | Sort-Object)) | Format-List
克里斯托弗是正确的,Select-Object
不是绝对需要的:
Christopher is right, Select-Object
is not absolutely needed:
$host | Format-List ([string[]]($host | Get-Member -MemberType Property | %{ $_.Name } | Sort-Object))
这篇关于格式列表:按名称对属性进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!