CmdID / PowerShell / Cmdlets / Group
Group-Object
Groups objects that contain the same value for specified properties. PowerShell reference
From the PowerShell reference
7 parameters
Alias group
What it does
Groups objects that contain the same value for specified properties. The Group-Object cmdlet displays objects in groups based on the value of a specified property. Group-Object returns a table with one row for each property value and a column that displays the number of items with that value. If you specify more than one property, Group-Object first groups them by the values of the first property, and then, within each property group, it groups by the value of the next property.
Group-Object [-NoElement] [-AsHashTable] [-AsString] [-InputObject <PSObject>] [[-Property] <Object[]>] [-Culture <String>] [-CaseSensitive] [<CommonParameters>]
Group files by extension
$files = Get-ChildItem -Path $PSHOME -Recurse
$files |
Group-Object -Property Extension -NoElement |
Sort-Object -Property Count -DescendingIdentifyGroup integers by odds and evens
1..20 | Group-Object -Property {$_ % 2}IdentifyGroup event log events by EntryType
Get-WinEvent -LogName System -MaxEvents 1000 | Group-Object -Property LevelDisplayNameIdentifyTrips 1 Sigma rule: mediumEventLog Query Requests By Builtin Utilities by Ali Alwashali, Nasreddine Bencherchali (Nextron Systems)
Group processes by priority class
Get-Process | Group-Object -Property PriorityClassIdentifyGroup processes by name
Get-Process | Group-Object -Property Name -NoElement | Where-Object {$_.Count -gt 1}IdentifyGroup objects in a hash table
$A = Get-Command Get-*, Set-* -CommandType Cmdlet |
Group-Object -Property Verb -AsHashTable -AsString
$AIdentifyGroup hashtables by their key values with calculated properties
@(
@{ name = 'a' ; weight = 7 }
@{ name = 'b' ; weight = 1 }
@{ name = 'c' ; weight = 3 }
@{ name = 'd' ; weight = 7 }
) | Group-Object -Property { $_.weight } -NoElementIdentifyIts page in the reference: Group-Object.
Parameters
-AsHashTableIndicates that this cmdlet returns the group as a hash table. The keys of the hash table are the property values by which the objects are grouped. The values of the hash table are the objects that have that property value.
By itself, the AsHashTable parameter returns each hash table in which each key is an instance of the grouped object. When used with the AsString parameter, the keys in the hash table are strings.-AsStringIndicates that this cmdlet converts the hash table keys to strings. By default, the hash table keys are instances of the grouped object. This parameter is valid only when used with the AsHashTable parameter.-CaseSensitiveIndicates that this cmdlet makes the grouping case-sensitive. Without this parameter, the property values of objects in a group might have different cases.-CultureSpecifies the culture to use when comparing strings.-InputObjectSpecifies the objects to group. Enter a variable that contains the objects, or type a command or expression that gets the objects.
When you use the InputObject parameter to submit a collection of objects to Group-Object,
Group-Object receives one object that represents the collection. As a result, it creates a single
group with that object as its member.
To group the objects in a collection, pipe the objects to Group-Object.-NoElementIndicates that this cmdlet omits the members of a group from the results.-PropertySpecifies the properties for grouping. The objects are arranged into named groups based on the value of the specified properties. When no property is specified, objects are grouped by their value or the ToString() representation of their value. The output is presented in order the group objects were created.
The value of the Property parameter can be a new calculated property. The calculated property can be a scriptblock or a hashtable. Valid key-value pairs are:
- Expression - <string> or <scriptblock>
For more information, see about_Calculated_Properties.