Format-List: sort properties by name

2020-04-02 09:05发布

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

I would like to have

(PS) > $x | Format-List 
A : value a
B : value 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.

8条回答
手持菜刀,她持情操
2楼-- · 2020-04-02 09:28

If you are dealing with a small number of properties, you can specify their order with the -Property parameter.

Here is an example:

Format-List -Property Owner, Path

If you have a lot of properties, I am not sure there is any easy way to sort them in Format-List, like Roman said.

查看更多
Viruses.
3楼-- · 2020-04-02 09:30

The closest I can think of is to create a new psobject based off the old one but with the properties sorted e.g.:

$x | %{$obj = new-object psobject; `
       $_.psobject.properties | Sort Name | `
           %{Add-Member -Inp $obj NoteProperty $_.Name $_.Value}; $obj} | fl

You could get fancier and give the new psobject a typename that matches the old one, etc.

查看更多
唯我独甜
4楼-- · 2020-04-02 09:31

Expanding on Christopher's idea, using get-member and format-list -Property:

$x | fl -property ($x| gm | sort name).name
查看更多
走好不送
5楼-- · 2020-04-02 09:32

This seems to work OK (edited so it accepts pipeline input):

function Format-SortedList
{
    param (
        [Parameter(ValueFromPipeline = $true)]
        [Object]$InputObject,
        [Parameter(Mandatory = $false)]
        [Switch]$Descending
    )

    process
    {
        $properties = $InputObject | Get-Member -MemberType Properties

        if ($Descending) {
            $properties = $properties | Sort-Object -Property Name -Descending
        }

        $longestName = 0
        $longestValue = 0

        $properties | ForEach-Object {
            if ($_.Name.Length -gt $longestName) {
                $longestName = $_.Name.Length
            }

            if ($InputObject."$($_.Name)".ToString().Length -gt $longestValue) {
                $longestValue = $InputObject."$($_.Name)".ToString().Length * -1
            }
        }

        Write-Host ([Environment]::NewLine)

        $properties | ForEach-Object { 
            Write-Host ("{0,$longestName} : {1,$longestValue}" -f $_.Name, $InputObject."$($_.Name)".ToString())
        }
    }
}

$Host, $MyInvocation | Format-SortedList
$Host, $MyInvocation | Format-SortedList -Descending
查看更多
一纸荒年 Trace。
6楼-- · 2020-04-02 09:41

AFAIK, Format-List does not provide such an option.

For your particular example this should work:

$x | Select-Object A, B | Format-List

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.

EDIT:

Here it is (let's use $host instead of $x):

$host | Select-Object ([string[]]($host | Get-Member -MemberType Property | %{ $_.Name } | Sort-Object)) | Format-List

Christopher is right, Select-Object is not absolutely needed:

$host | Format-List ([string[]]($host | Get-Member -MemberType Property | %{ $_.Name } | Sort-Object))
查看更多
够拽才男人
7楼-- · 2020-04-02 09:42

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.

查看更多
登录 后发表回答