How to sort PowerShell Format-List result?

SAM2009
SAM2009 used Ask the Experts™
on
Hi,

How to get the result of this cmd but sorted:

Get-Recipient -RecipientType MailContact -Identity "UserName" | fl
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
You can't. You don't. You're not supposed to.
The Format-* cmdlets are (usually) meant to be the last element of the pipeline. They only format the output, basically destroying the original objects.
The solution: sort before the Format-* cmdlets.
Get-Recipient -RecipientType MailContact -Identity "UserName" | Sort-Object -Property Whatever | Format-List

Open in new window

Author

Commented:
No it doesn't sort. Same result.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Of course you need to tell which property to use for sorting. "Whatever" is a placeholder ;-).
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Well, it would sort, but it could be that you selected a property that's not sortable.
Then you can go a similar route like with calculated properties: pass a hash table with an expression that creates the value on which to sort.
Example: sort the Windows directory based on the second character in the name:
Get-ChildItem C:\Windows | Sort-Object -Property @{e={$_.Name[1]}}

Open in new window

Try something like
Get-Recipient -RecipientType MailContact -Identity "UserName" | Sort-Object -Property @{e={"$($_.Whatever)"}} | Format-List

Open in new window

Author

Commented:
Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial