Using PowerShell Hash Tables with -Property and -GroupBy


When learning these, I struggled to find authoritative lists for what cmdlets supported what hashtable keys. The man pages for cmdlets sometimes listed them, but not often. Sometimes the man pages were wrong (for example, the nonexistent depth= key).

Note that the name of the key can be abbreviated to any length. For example, alignment="right" and align="right" and al="right" and a="right" are all allowed. Usually, we abbreviate to just the first letter.

The name= and label= keys are synonyms. Use whichever you prefer.

Summary

Cmdlet name expression ascending,
descending
width formatstring alignment
Select-Object
   -Property
Sort-Object
   -Property ✓ ¹
Format-Table
   -Property
   -GroupBy ✓ ²
Format-List
   -Property
   -GroupBy ✓ ²
Format-Wide
   -Property
   -GroupBy ✓ ²

Notes

¹ If you use both ascending= and descending= then the descending value takes precedence.
² The display of the grouping does not, however, seem to honour the format string.

Select-Object

The –Property parameter supports: name/label = "string"; expression = {script block}

Get-Process `
| Select-Object `
    -Property `
        @{name="Taskname";expression={$PSItem.Name}}, `
        @{n="PID";e={$PSItem.Id}}


Sort-Object

The –Property parameter supports: expression = {script block}; ascending = Boolean; descending = Boolean 

Get-Process `
| Sort-Object `
    -Property `
        @{expression={$PSItem.Name};descending=$true}, `
        @{e={$PSItem.Handles};ascending=$true}

Format-Table

The –Property parameter supports: name/label = "string"; expression = {script block}; width = Int; formatstring = ".NET format string"; alignment = "left" | "center" | "right”

The –GroupBy parameter supports: name/label = "string"; expression = {script block}; formatstring = ".NET format string"

Get-Process `
| Format-Table `
    -Property `
        @{n="Process Name";e={$PSItem.ProcessName};width=50}, `
        @{n="WS (MB)";e={$PSItem.ws/1MB};alignment="left";formatstring="n2";width=12}, `
        @{n="VM (MB)";e={$PSItem.vm/1MB};a="left";f="n2";w=12} 

Get-Process | Sort-Object -Property @{e={$PSItem.Threads.count}} `
| Format-Table `
    -GroupBy `
        @{n="Threads";e={$PSItem.Threads.count};f="n1"} `
    -Property `
        @{n="Process Name";e={$PSItem.ProcessName};w=50}, `
        @{n="WS (MB)";e={$PSItem.ws/1MB};a="left";f="n2";w=12}, `
        @{n="VM (MB)";e={$PSItem.vm/1MB};a="left";f="n2";w=12} 

Format-List

The –Property and –GroupBy parameters support: name/label = "string"; expression = {script block}; formatstring = ".NET format string"

Get-Process `
| Format-List `
    -Property `
        @{n="Process Name";e={$PSItem.processname}},
        @{n="WS (MB)";e={$PSItem.ws/1MB};formatstring="n2"} ,
        @{n="VM (MB)";e={$PSItem.vm/1MB};f="n2"} 

Get-Process | Sort-Object -Property @{e={$PSItem.Threads.count}} `
| Format-List `
    -GroupBy `
        @{n="Threads";e={$PSItem.Threads.count};f="n1"} `
    -Property `
        @{n="Process Name";e={$PSItem.processname}},
        @{n="WS (MB)";e={$PSItem.ws/1MB};f="n2"} ,
        @{n="VM (MB)";e={$PSItem.vm/1MB};f="n2"} 

Format-Wide

The –Property parameter supports: expression = {script block}; formatstring = ".NET format string"

The –GroupBy parameter supports: name/label = "string"; expression = {script block}; formatstring = ".NET format string"

Get-Process `
| Format-Wide `
    -AutoSize `
    -Property `
        @{e={$PSItem.ws/1MB};formatstring="n2"} 

Get-Process | Sort-Object -Property @{e={$PSItem.Threads.count}} `
| Format-Wide `
    -AutoSize `
    -GroupBy `
        @{n="Threads";e={$PSItem.Threads.count};f="n1"} `
    -Property `
        @{e={$PSItem.ws/1mb};f="n2"} 

Links