PowerShell Genie

Group-Object

Use Group-Object to group objects that share a value for one or more properties, and count each group.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Group-Object

Syntax

1PS> Group-Object [[-Property] <object[]>] [-NoElement] [-AsHashTable] [-AsString] [-CaseSensitive] [-Culture <String>] [-InputObject <PSObject>] [<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 | Group-Object Name | Sort-Object Count -Descending | Select-Object -First 3 Count, Name
  1. Stage 1Get-ProcessPositional values or no named parameters
  2. Stage 2Group-ObjectPositional values or no named parameters
  3. Stage 3Sort-Object-Descending
  4. Stage 4Select-Object-First

    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

    Get-Service -NoElement

    1PS> Get-Service | Group-Object Status -NoElement
    Show reference output

    Illustrative output · values vary by environment

    Count  Name     Group
    -----  ----     -----
    3      Running  {BITS, Dnscache, WinRM}
    1      Stopped  {Spooler}

    Sort by Count

    1PS> Get-Process | Group-Object Name | Sort-Object Count -Descending | Select-Object -First 3 Count, Name
    Show reference output

    Illustrative output · values vary by environment

    Count  Name   
    -----  -------
    1      chrome 
    1      notepad
    1      pwsh   

    Sort by Count (2)

    1PS> Get-ChildItem 'C:\Scripts' | Group-Object Extension -NoElement | Sort-Object Count -Descending
    Show reference output

    Illustrative output · values vary by environment

    Count  Name
    -----  ----
    2      SampleA
    1      SampleB

    Parameters

    Parameter definitions:

    • -Property
    • -NoElement
    • -AsHashTable
    • -AsString
    • -CaseSensitive
    • -Culture
    • -InputObject

    Group-Object on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use Group-Object when you need to group objects that share a value for one or more properties, and count each group. 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: An unexpected group key may come from inconsistent whitespace or missing values. Inspect the key property and normalise only when the reporting requirement defines those values as equivalent. 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: Sort-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.

    Group-Object reference on Microsoft Learn

    Put this command to work

    Group services by status

    Group service objects by Status and return the grouping records.

    Review the lesson

    Recap

    Group-Object Use Group-Object to group objects that share a value for one or more properties, and count each group.

    Next: ForEach-Object
    Genie guideGroup-Object

    Use Group-Object to group objects that share a value for one or more properties, and count each group. The console below is pre-loaded — run it, then change a value and run it again.