I want to produce a table of process names and total CPU times sorted by total CPU time. The following works as expected:
ps | sort -p cpu | select processname,cpu
But if I reverse the direction of sorting as follows, the CPU time column disappears:
ps | sort -descending -p cpu | select processname,cpu
Why is this?
It seems that the CPU property is sometimes a Double and sometimes null. When I ran the first command, the first item had null CPU and the column does get displayed: for the second command, the first item has a Double-valued CPU and the column doesn't get displayed.
When the column doesn't get displayed it still exists! Using Format-List, shows it, for example:
ps | sort -descending -p cpu | select processname,cpu | fl
What is going on?
The Sort-Object cmdlet sorts objects in ascending or descending order based on object property values. If sort properties aren't included in a command, PowerShell uses default sort properties of the first input object.
To sort the output in the PowerShell you need to use Sort-Object Pipeline cmdlet. In the below example, we will retrieve the output from the Get-Process command and we will sort the, according to memory and CPU usage.
Sort array: It's very easy of arranging the elements of an array in a order with PowerShell. Just we need to do is pipe the output of an array to the Sort-Object cmdlet: The default sort order is ascending : the numbers range from small to large. To perform a descending sort requires utilizing the Descending switch.
Try this
ps | sort -descending -p cpu | select processname,cpu | Format-Table -AutoSize
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With