SOLVED

How to sort when using custom tables

%3CLINGO-SUB%20id%3D%22lingo-sub-814739%22%20slang%3D%22en-US%22%3EHow%20to%20sort%20when%20using%20custom%20tables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-814739%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3EI%20have%20a%20registry%20hives%20where%20I%20have%20same%20information%20on%20all%20of%20them.%20I%20would%20like%20to%20read%20the%20values%20from%20all%20of%20those%20hives%2C%20and%20then%20sort%20output%20based%20on%20the%20values.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20I%20have%20simple%20example%20of%20the%20similar%20CMDLet%3A%3C%2FP%3E%3CP%3E%3CFONT%20face%3D%22courier%20new%2Ccourier%22%20color%3D%22%230000FF%22%3EGet-ChildItem%20-Path%20HKLM%3A%5CSYSTEM%5CCurrentControlSet%5CServices%20-Depth%200%20%7C%20format-table%20%40%7BLabel%3D%22Services%22%3B%20Expression%3D%7B(Get-ItemProperty%20-Path%20%24_.PSPath).DisplayName%7D%7D%2C%20%40%7BLabel%3D%22Start%22%3B%20Expression%3D%7B(Get-ItemProperty%20-Path%20%24_.PSPath).Start%7D%7D%3C%2FFONT%3E%3C%2FP%3E%3CP%3EAbove%20basically%20read%20the%20services%20and%20print%20out%20the%20displayname%20and%20start%20type.%20But%20I%20realised%20that%20sort-object%20is%20not%20working%2C%20neither%20convertto-csv.%20Any%20ideas%20if%20this%20is%20even%20doable%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-814739%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-815464%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20sort%20when%20using%20custom%20tables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-815464%22%20slang%3D%22en-US%22%3E%3CP%3EThat's%20because%20you%20are%20using%20Format-Table.%20Use%20a%20simple%20Select%20instead.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EGet-ChildItem%20-Path%20HKLM%3A%5CSYSTEM%5CCurrentControlSet%5CServices%20-Depth%200%20%7C%20Select%20%40%7BLabel%3D%22Services%22%3B%20Expression%3D%7B(Get-ItemProperty%20-Path%20%24_.PSPath).DisplayName%7D%7D%2C%20%40%7BLabel%3D%22Start%22%3B%20Expression%3D%7B(Get-ItemProperty%20-Path%20%24_.PSPath).Start%7D%7D%20%7C%20sort%20Start%20-Descending%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-816821%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20sort%20when%20using%20custom%20tables%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-816821%22%20slang%3D%22en-US%22%3E%3CP%3EBig%20thanks%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3B%20!%3C%2FP%3E%3CP%3ENow%20I%20have%20learnt%20once%20more%20something%20new%20%3AD%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Super Contributor

Hi

I have a registry hives where I have same information on all of them. I would like to read the values from all of those hives, and then sort output based on the values.

 

Here I have simple example of the similar CMDLet:

Get-ChildItem -Path HKLM:\SYSTEM\CurrentControlSet\Services -Depth 0 | format-table @{Label="Services"; Expression={(Get-ItemProperty -Path $_.PSPath).DisplayName}}, @{Label="Start"; Expression={(Get-ItemProperty -Path $_.PSPath).Start}}

Above basically read the services and print out the displayname and start type. But I realised that sort-object is not working, neither convertto-csv. Any ideas if this is even doable?

2 Replies
Highlighted
Best Response confirmed by Petri X (Super Contributor)
Solution

That's because you are using Format-Table. Use a simple Select instead.

 

Get-ChildItem -Path HKLM:\SYSTEM\CurrentControlSet\Services -Depth 0 | Select @{Label="Services"; Expression={(Get-ItemProperty -Path $_.PSPath).DisplayName}}, @{Label="Start"; Expression={(Get-ItemProperty -Path $_.PSPath).Start}} | sort Start -Descending
Highlighted

Big thanks @Vasil Michev  !

Now I have learnt once more something new :D