Sorting of PowerShell objects (combined with hash tables)
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.