Get the App
SLTechnology News&Howtos  ›  Servers  › 

Sorting of PowerShell objects (combined with hash tables)

Shulou Source: shulou.com Published: 2022-06-03 03:11:26 09月22日 Update

You can sort different attributes in a different order by using a hash table array.

Get-ChildItem | Sort-Object-Property @ {Expression = 'LastWriteTime'; Descending = $true}, @ {Expression =' Name'; Ascending = $true} | Format-Table-Property LastWriteTime, Name

To improve readability, you can put the hash table into a separate variable:

$order = @ (@ {Expression = 'LastWriteTime'; Descending = $true} @ {Expression =' Name'; Ascending = $true}) Get-ChildItem | Sort-Object $order | Format-Table LastWriteTime, Name

The keys for sorting in the hash table can be abbreviated as follows:

Sort-Object @ {e = 'LastWriteTime'; d = $true}, @ {e =' Name'; a = $true}

You can also follow the official Wechat account below for more information.

Tags: Hash sort different public variable can be passed readability properties array more information order reducible object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple Shulou Information Linux OPPO Reno