PowerShell – Sorting Objects

In PowerShell, when you run a cmdlet, script determines the order of output objects itself.I mean if execute a script which retrieves list of processes;

image

 

PowerShell displays processes in alphabetical order by name column.This is a default behavior for get-process cmdlet in PowerShell.Another cmdlets have another order types.

If you want to change this default display, you must modify script with sort-object cmdlet.

Sort-object changes the order of output objects.By default sort order is ascending.you can specify this too with –descending parameter.

Let’s see.

Get-Process | Sort-Object Handles

image

The output objects is now ordered by Handles property.

Get-Process | Sort-Object Handles –descending

image

Also you can specify multiple properties.

Get-Process | Sort-Object Handles, ID

List of processes sorted by Handles, but sorted by ID within each Handles.

Leave a Reply

Your email address will not be published. Required fields are marked *

4 + 4 =