Group-Object
Use Group-Object to group objects that share a value for one or more properties, and count each group.
Documentation: Microsoft Learn — Group-Object
Syntax
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- Stage 1Get-Process
Positional values or no named parameters - Stage 2Group-Object
Positional values or no named parameters - Stage 3Sort-Object
-Descending - Stage 4Select-Object
-First
Try it
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.
Follow the objects
Run the command to inspect each stage.
Examples
Get-Service -NoElement
Show reference output
Illustrative output · values vary by environment
Count Name Group
----- ---- -----
3 Running {BITS, Dnscache, WinRM}
1 Stopped {Spooler}Sort by Count
Show reference output
Illustrative output · values vary by environment
Count Name ----- ------- 1 chrome 1 notepad 1 pwsh
Sort by Count (2)
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
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.
Put this command to work
Group services by status
Group service objects by Status and return the grouping records.
The sandbox is visible now. Sign in with Google only when you run or check a task; commands and temporary files are discarded when you leave.
Run a command to inspect its result.
Inspect starting data
Supported syntax
Virtual files
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