Group-Object

Group objects that contain the same value.

Syntax
      Group-Object [-AsHashTable] [-AsString] [[-property] Object[]]
         [-caseSensitive] [-noElement] [-culture string]
            [-inputObject psobject] [CommonParameters]

Key
   -AsHashTable
       Return 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, -AsHashTable returns each hash table in which each key is an instance
       of the grouped object. When used with -AsString, the keys in the hash table are strings.

   -AsString
       Convert 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 -AsHashTable.

   -property Object[]
       The property or list of properties upon which to group.
       The value of -Property can be a new calculated property.
       To create a calculated, property, create a hash table with an Expression key
       that specifies a string or script block value.

   -caseSensitive
       Make the grouping case-sensitive.
       e.g. Test, TEST and TesT would become separate groups.
        
   -noElement
       Don’t include members of each group in the output objects.

   -culture string
       The culture to use when performing a string comparison.
        
   -inputObject psobject
       The objects to group. Enter a variable that contains the objects or type
       an expression that returns the objects.

       When the -InputObject parameter is used to submit a collection of items,
       Group-Object receives one object that represents the collection.
       Because one object cannot be grouped, Group-Object returns the entire collection unchanged.

       To group multiple items, pipe them to Group-Object.

       This parameter is an implementation detail: its purpose is to enable input via the pipeline, and its
       direct use with arrays (collections) does not (yet) provide any useful functionality.

Standard Aliases for Group-Object: group

The output of group-object can be passed down the pipeline for further processing, use ForEach-Object to access each group which will have the properties: $_.count, $_.name and $_.group

Examples

Display a list of files grouped by size:

PS C:\> get-childitem *.doc | group-object -property length<

Display a list of files, sorted and then grouped by the file extension:

PS C:\> get-childitem | sort-object -property extension | group-object -property extension

Count the file extensions (in the current directory) without retrieving all the files:

PS C:\> get-childitem | group-object extension -noelement

Group a set of files by the first 3 characters of the filename and save the result in a hash table of strings:

PS C:\> hfil = get-childitem c:\demo\ | `
group-object -property {$_.name.substring(0,3)} -AsHashTable -AsString

Now display only the files that start with 'abc':

PS C:\> $hfil.'abc'

“ We must remember that one determined person can make a significant difference, and that a small group of determined people can change the course of history” ~ Sonia Johnson

Related PowerShell Cmdlets

Compare-Object Compare the properties of objects.
ForEach-object - Loop for each object in the pipeline.
Measure-Object - Measure aspects of object properties and create objects from those values.
New-Object - Create a new .Net object
Select-Object - Select objects based on parameters set in the Cmdlet command string.
Sort-Object - Sort the input objects by property value.
Tee-Object - Send input objects to two places.
Where-Object - Filter input from the pipeline allowing operation on only certain objects
.


 
Copyright © 1999-2024 SS64.com
Some rights reserved