Forum Discussion

Petri-X's avatar
Petri-X
Bronze Contributor
Aug 21, 2019
Solved

How to sort when using custom tables

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?

  • 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

2 Replies

  • 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