Format-List: sort properties by name

后端 未结 8 1975
一向
一向 2021-01-17 18:02

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

8条回答
  •  醉梦人生
    2021-01-17 18:20

    Nothing wrong with the accepted answer, but a really quick-and-dirty option for a one-off—that doesn't require having the collection already in a variable—might be...

    ... | Format-List | Out-String -Stream | Sort-Object
    

    ...which does a sort on each line of the output of Format-List.

    Note that any property values that go onto the next line will be broken (and probably appear at the top of the output), but this could be fixed by the slightly-less-memorable...

    ... | Format-List | Out-String -Stream -Width ([Int32]::MaxValue) | Sort-Object
    

    ...at the expense of column indentation.

    Of course, all object/pipeline info is lost by that Out-String call, although—considering the same is true of Format-List—you probably aren't going to care by that point.

提交回复
热议问题