Group-Object

 

Additional Resources for Group-Object

 

Arranging Data Into Groups

http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/group-object.mspx

 

 

SYNOPSIS

Groups objects that contain the same value for specified properties.

 

SYNTAX

Group-Object [[-property] <Object[]>] [-caseSensitive] [-noElement] [-culture <string>] [-inputObject <psobject>] [<CommonParameters>]

 

DETAILED DESCRIPTION

Groups objects that contain the same value for specified properties. By default, the cmdlet creates a GroupInfo object for each set of input objects that share the same value for the properties specified in the Property parameter. The GroupInfo object has a group property. That property is a collection of all the input objects that are members of the group represented by the GroupInfo object. The number of members of a group is stored in the Count property of the corresponding GroupInfo object and the group has a Name that is derived from the value of their common properties.

 

To omit the input objects from the results, specify the NoElement parameter. The results will be an array of GroupInfoNoElement objects that each have an empty Group property. By default, comparisons made to establish groups are not case sensitive. You can change this by specifying the CaseSensitive parameter.

 

PARAMETERS

 

-property <Object[]>

Specifies the property or list of properties upon which to group the input objects.

 

Required?

false

Position?

1

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-caseSensitive <SwitchParameter>

Specifies that case should be taken into account when determining whether property values are equal for the purpose of grouping.

 

Required?

false

Position?

named

Default value

False

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-noElement <SwitchParameter>

Determines whether the output objects contain the elements of each group.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-culture <string>

Specifies the culture to use when performing string comparisons.

 

Required?

false

Position?

named

Default value

Current Culture

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-inputObject <psobject>

Specifies the objects to group. Enter a variable that contains the objects or type a command or expression that gets the objects.

 

Required?

false

Position?

named

Default value

 

Accept pipeline input?  

true (ByValue)

Accept wildcard characters? 

false

 

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. For more information, type, "get-help about_commonparameters".

 

INPUT TYPE

Object stream

 

RETURN TYPE

Objects

 

NOTES

 

Group-Object does not require that the objects being grouped are of the same type. The cmdlet uses the following rules when grouping objects of different types.

 

Same Property Names and Types: If the objects have the specified properties and they are of the same type in all the objects, then they are grouped using the same rules as objects of the same type.

 

Same Property Names, Different Types: If a property with the specified name exists in all objects but is a different type in different objects, the type of the first occurrence of the property in a group of objects is taken as the base type for that property. When there is an occurrence of a different type, the value of the type is converted to the base type for each group. If the type conversion fails, then the object does not match that group.

 

Missing Properties: Objects that do not have a specified property are considered ungroupable. Ungroupable objects appear in the final GroupInfo object output in a group named AutomationNull.Value.

 

EXAMPLE 1

 

get-childitem *.doc | group-object -property length

 

This command displays a list of all the files in the current location that have a .doc extension and groups them by size.

 

EXAMPLE 2

 

get-childitem | sort-object -property extension | group-object -property extension

 

This command displays a list of all the files in the current location, sorted and then grouped by file extension. Note the sort operation was performed before the group operation.

 

EXAMPLE 3

 

1..35 | group-object -property {$_ % 2},{$_ % 3}

 

This command displays the integers from 1 to 35, grouped by the remainder left when they are divided by 2 or 3. It demonstrates that the cmdlet can accept multiple script blocks as input.

 

EXAMPLE 4

 

$events = get-eventlog -logname system -newest 1000

$events | group-object -property eventID

 

This command displays the most recent 1000 entries from the system event log, grouped by Event ID. It uses the Get-EventLogcmdlet to retrieve and store the events in the $events variable. In the second line, it pipelines the stored events to the Group-Object cmdlet, using the Property parameter to specify that the events should be grouped according to the value of their eventID properties. In the output, the Count column represents the number of entries in each group, the Name column represents the EventID values that each define a group and the Group column represents the property of the output object that contains an array of the objects in each group.

 

Count Name  Group

----- ----  -----

247   7036  {FABRIKAM, FABRIKAM, FABRIKAM,..

361   30    {FABRIKAM, FABRIKAM, FABRIKAM,..

 

EXAMPLE 5

 

get-process | group-object -property company -noelement

 

This command displays a list of all the processes on the computer, grouped by the name of the company whose application is associated with the process. Because the NoElement parameter is used, the object returned by the command does not include the process objects that were grouped.

 

EXAMPLE 6

 

get-eventlog -logname system -newest 1000 |

group-object -property {$_.TimeWritten - $_.TimeGenerated}

 

This command displays the most recent 1000 entries from the system event log, grouped according the time between when they were generated and when they were written to the log. The command uses the Get-EventLogcmdlet to retrieve the entries and pipelines them to the Group-Object cmdlet. The value of the Property parameter is specified as a script block (an expression in braces). The result of evaluating the script block is the time between when the log entry was generated and when it was written to the log. That value is used to group the 1000 most recent events. This command demonstrates that you can provide the value of the Property parameter as a script block.

 

EXAMPLE 7

 

get-childitem | group-object extension -noelement

 

This command determines which file extensions we have in the current directory without retrieving the files belonging to each group.

 

EXAMPLE 8

 

$events = get-eventlog -logname application -newest 500

$groups = group-object -inputobject $events -property entrytype,source

$groups | get-member

$groups | select-object -expandproperty group

 

This command creates GroupInfo objects that represent the grouping of events from the application event log. The Get-EventLogcmdlet is used to retrieve the 500 most recent events from the Application event log and store them in the $events variable. The $events variable is passed as input to the Group-Object cmdlet and the events are grouped according to entry type and event source and the resulting groups are stored in the $groups variable. The $groups variable is piped to the Get-Membercmdlet to display information about it. In the last line of the command, the $groups variable is piped to the Select-Objectcmdlet. The ExpandProperty parameter is used to display the event information stored in the group property of the GroupInfo objects stored in the $groups variable.

 

RELATED LINKS

Sort-Object

Measure-Object

New-Object

Select-Object

ForEach-Object

Where-Object

Compare-Object

Tee-Object