colonel_claypoo
colonel_claypoo

Reputation: 615

Possible to sort object by property (descending) and still display corresponding values?

$procs = Get-Process
$procs[0] | GM -MemberType Property

This gives all the properties in ascending order. Now, I'd like to display the properties in descending order but with it's values. It's easy to sort the properties themselves...

$procs[0] | GM -MemberType Property | Select-Object Name | Sort-Object -Descending -Property Name

...however this does not display the corresponding values.

I'm also able to sort values of one property in a descending manner but not sort by properties themselves.

EDIT: Here's a screenshot to better illustrate what I'd like to achieve:

enter image description here

What you can see here is the sample output of Get-Process | Where-Object { $_.Name -like "a*" } | Export-Csv -Path "$env:USERPROFILE\Desktop\example.csv"

As you can see the properties are not sorted. I would like to be able to output exactly what the screenshot shows but with the property columns and their corresponding values sorted in descending order, i.e. WS, VM, SI, PM, NPM, Name, Handles.

I hope I was able to clearly explain it now.

Thanks for your help.

Upvotes: 0

Views: 505

Answers (1)

FletcherF1
FletcherF1

Reputation: 187

It happens because, after all in your pipeline result is Hashtable with strings. Save result in variable and give it into pipe like this,it's reveal each property in detail by putting Name of property after variable In "dot notation"

$propertys = (get-process)[0] | GM -MemberType Property | Select-Object Name | Sort-Object -Descending -Property Name
($propertys.NAME) | %{(get-process)[0].$_}

or use short presentation of properties by using FL

(get-process)[0] | fl $($propertys.Name[0..$propertys.Count])

Upvotes: 1

Related Questions