liveBook · Manning

Sort-Object

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

Sort-Object is a PowerShell cmdlet that orders input objects according to specified property values.

Sort-Object is a PowerShell cmdlet for changing the order of input objects according to one or more property values. For example, it can put processes with the highest CPU usage first. section 8.6

Use the cmdlet to control ordering in a pipeline:

  • Sort processes by CPU with Get-Process | Sort-Object CPU. section 8.6
  • Add -Descending, or its abbreviation -desc, to place the largest values first. section 8.6
  • Provide comma-separated properties such as CPU,ID to use a secondary ordering when values tie. section 8.6
  • Sort files by CreationTime or LastWriteTime before selecting, exporting, or writing their results. section 8.11

Sort processes for review

Get-Process | Sort-Object CPU -Descending | Select-Object Name,ID,CPU sorts process objects by CPU while retaining them in the pipeline, then creates a smaller display object with the selected properties. section 8.8

Get-Process | Sort-Object CPU,ID -desc

Compare primary and secondary property sorting with descending order using a compact, runnable process pipeline. section 8.6

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains property selection, descending order, multiple sort keys, and preservation of original pipeline objects.
Examples
Demonstrates sorting processes by CPU, files by timestamps, and remote results before selecting or exporting them.
What it assumes
Assumes familiarity with PowerShell commands, pipelines, objects, and object properties.
What you can do after
Apply it via PowerShell pipelines to order objects by one or more properties before displaying or exporting results.
How it connects
Connects Sort-Object with PowerShell pipelines, Get-Process, Select-Object, Out-File, and remote commands.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.