PowerShell Genie

Sort-Object

Use Sort-Object to order objects by property, ascending by default or -Descending; -Unique drops duplicates.

Command 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Sort-Object

Syntax

1PS> Sort-Object [[-Property] <object[]>] [-Descending] [-Unique] [<CommonParameters>]
See the command anatomy

Inspect this authored example with fictional data in the bounded browser model. The numbered stages show how its objects change.

Get-Process | Sort-Object CPU -Descending
  1. Stage 1Get-ProcessPositional values or no named parameters
  2. Stage 2Sort-Object-Descending

    The object workshop

    Watch your command work.

    Change the command. Step through the pipeline. Inspect what survives.

    5 fictional services

    Browser simulation · fictional services · Ctrl+Enter to run

      Object properties

      Enable JavaScript to step through this example. The lessons and command reference remain available.

      Your task: return the stopped service, with exactly Name and Status.

      Try it

      Windows PowerShell

      PowerShell example · browser support is limited.

      Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

      Interactive simulation · fictional data

      Examples

      Sort by CPU

      1PS> Get-Process | Sort-Object CPU -Descending
      Show reference output

      Illustrative output · values vary by environment

      Name     ProcessName  Id     CPU     WS         WorkingSet  WorkingSet64
      -------  -----------  -----  ------  ---------  ----------  ------------
      MsMpEng  MsMpEng      2600   950     220200960  220200960   220200960   
      chrome   chrome       9184   842.31  486539264  486539264   486539264   
      pwsh     pwsh         11268  18.27   78643200   78643200    78643200    
      spoolsv  spoolsv      4400   1.4     33554432   33554432    33554432    
      notepad  notepad      3200   0.4     12582912   12582912    12582912    

      Sort by Status

      1PS> Get-Service | Sort-Object Status,Name
      Show reference output

      Illustrative output · values vary by environment

      Status   Name      DisplayName
      ------   ----      -----------
      Running  BITS      Background Intelligent Transfer Service
      Running  Dnscache  DNS Client
      Running  WinRM     Windows Remote Management
      Stopped  Spooler   Print Spooler

      Parameters

      Parameter definitions:

      • -Property
      • -Descending
      • -Unique

      Sort-Object on Microsoft Learn

      Practical use and troubleshooting

      Practical scenario: Use Sort-Object when you need to order objects by property, ascending by default or -Descending; -Unique drops duplicates. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

      Common error and fix: Numbers stored as strings can sort lexically rather than numerically. Inspect the property type and convert the value deliberately before sorting when numeric order is required. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

      See also: Group-Object. Sort-Object orders objects by property values; Group-Object collects objects sharing a key. A grouped result contains groups and is not the original flat list in a new order. Read the linked command before substituting it; similar names do not imply identical inputs or output.

      Sort-Object reference on Microsoft Learn

      Put this command to work

      Sort service names

      Return all services ordered by Name.

      Review the lesson

      Recap

      Sort-Object Use Sort-Object to order objects by property, ascending by default or -Descending; -Unique drops duplicates.

      Next: Measure-Object
      Genie guideSort-Object

      Use Sort-Object to order objects by property, ascending by default or -Descending; -Unique drops duplicates. The console below is pre-loaded — run it, then change a value and run it again.