ערוך

שתף באמצעות


Group-Object

Groups objects that contain the same value for specified properties.

Syntax

Group-Object
     [-NoElement]
     [-AsHashTable]
     [-AsString]
     [-InputObject <PSObject>]
     [[-Property] <Object[]>]
     [-Culture <String>]
     [-CaseSensitive]
     [<CommonParameters>]

Description

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.

Examples

Example 1: Group files by extension

This example recursively gets the files under $PSHOME and groups them by filename extension. The output is sent to the Sort-Object cmdlet, which sorts them by the count files found for the given extension. The empty Name represents directories.

This example uses the NoElement parameter to omit the members of the group.

$files = Get-ChildItem -Path $PSHOME -Recurse
$files |
    Group-Object -Property extension -NoElement |
    Sort-Object -Property Count -Descending

Count Name
----- ----
  365 .xml
  231 .cdxml
  197
  169 .ps1xml
  142 .txt
  114 .psd1
   63 .psm1
   49 .xsd
   36 .dll
   15 .mfl
   15 .mof
...

Example 2: Group integers by odds and evens

This example shows how to use script blocks as the value of the Property parameter. This command displays the integers from 1 to 20, grouped by odds and even.

1..20 | Group-Object -Property {$_ % 2}

Count Name                      Group
----- ----                      -----
   10 0                         {2, 4, 6, 8...}
   10 1                         {1, 3, 5, 7...}

Example 3: Group event log events by EntryType

This example displays the 1,000 most recent entries in the System event log, grouped by EntryType.

In the output, the Count column represents the number of entries in each group. The Name column represents the EventType values that define a group. The Group column represents the objects in each group.

Get-WinEvent -LogName System -MaxEvents 1000 | Group-Object -Property LevelDisplayName

Count Name          Group
----- ----          -----
  153 Error         {System.Diagnostics.Eventing.Reader.EventLogRecord, System.Diag...}
  722 Information   {System.Diagnostics.Eventing.Reader.EventLogRecord, System.Diag...}
  125 Warning       {System.Diagnostics.Eventing.Reader.EventLogRecord, System.Diag...}

Example 4: Group processes by priority class

This example demonstrates the effect of the NoElement parameter. These commands group the processes on the computer by priority class.

The first command uses the Get-Process cmdlet to get the processes on the computer and sends the objects down the pipeline. Group-Objectgroups the objects by the value of the PriorityClass property of the process.

The second example uses the NoElement parameter to remove the members of the group from the output. The result is a table with only the Count and Name property value.

The results are shown in the following sample output.

Get-Process | Group-Object -Property PriorityClass

Count Name         Group
----- ----         -----
   55 Normal       {System.Diagnostics.Process (AdtAgent), System.Diagnosti...
    1              {System.Diagnostics.Process (Idle)}
    3 High         {System.Diagnostics.Process (Newproc), System.Diagnostic...
    2 BelowNormal  {System.Diagnostics.Process (winperf),

Get-Process | Group-Object -Property PriorityClass -NoElement

Count Name
----- ----
   55 Normal
    1
    3 High
    2 BelowNormal

Example 5: Group processes by name

The following example uses Group-Object to group multiple instances of processes running on the local computer. Where-Object displays processes with more than one instance.

Get-Process | Group-Object -Property Name -NoElement | Where-Object {$_.Count -gt 1}

Count Name
----- ----
2     csrss
5     svchost
2     winlogon
2     wmiprvse

Example 6: Group objects in a hash table

This example uses the AsHashTable and AsString parameters to return the groups in a hash table, as a collection of key-value pairs.

In the resulting hash table, each property value is a key, and the group elements are the values. Because each key is a property of the hash table object, you can use dot notation to display the values.

The first command gets the Get and Set cmdlets in the session, groups them by verb, returns the groups as a hash table, and saves the hash table in the $A variable.

The second command displays the hash table in $A. There are two key-value pairs, one for the Get cmdlets and one for the Set cmdlets.

The third command uses dot notation, $A.Get to display the values of the Get key in $A. The values are CmdletInfo object. The AsString parameter doesn't convert the objects in the groups to strings.

$A = Get-Command Get-*, Set-* -CommandType cmdlet |
     Group-Object -Property Verb -AsHashTable -AsString
$A

Name     Value
----     -----
Get      {Get-Acl, Get-Alias, Get-AppLockerFileInformation, Get-AppLockerPolicy...}
Set      {Set-Acl, Set-Alias, Set-AppBackgroundTaskResourcePolicy, Set-AppLockerPolicy...}

$A.Get

CommandType     Name                              Version    Source
-----------     ----                              -------    ------
Cmdlet          Get-Acl                           3.0.0.0    Microsoft.PowerShell.Security
Cmdlet          Get-Alias                         3.1.0.0    Microsoft.PowerShell.Utility
Cmdlet          Get-AppLockerFileInformation      2.0.0.0    AppLocker
Cmdlet          Get-AppLockerPolicy               2.0.0.0    AppLocker
...

Example 10: Group hashtables by their key values with calculated properties

This example shows how you can group hashtable objects by the value of their keys. You can specify one or more scriptblocks for the Property parameter. The expressions in these scriptblocks are used to group for the input like the values for named properties.

@(
    @{ name = 'a' ; weight = 7 }
    @{ name = 'b' ; weight = 1 }
    @{ name = 'c' ; weight = 3 }
    @{ name = 'd' ; weight = 7 }
) | Group-Object -Property { $_.weight } -NoElement

Count Name
----- ----
    2 7
    1 1
    1 3

Parameters

-AsHashTable

Indicates 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.

Type:SwitchParameter
Aliases:AHT
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-AsString

Indicates 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.

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-CaseSensitive

Indicates that this cmdlet makes the grouping case-sensitive. Without this parameter, the property values of objects in a group might have different cases.

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Culture

Specifies the culture to use when comparing strings.

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-InputObject

Specifies 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.

Type:PSObject
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-NoElement

Indicates that this cmdlet omits the members of a group from the results.

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Property

Specifies 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 script block or a hash table. Valid key-value pairs are:

  • Expression - <string> or <script block>

For more information, see about_Calculated_Properties.

Type:Object[]
Position:0
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

Inputs

PSObject

You can pipe any object to this cmdlet.

Outputs

GroupInfo

By default, this cmdlet returns a GroupInfo object.

Hashtable

When you use the AsHashTable parameter, this cmdlet returns a Hashtable object.

Notes

Windows PowerShell includes the following aliases for Group-Object:

  • group

You can use the GroupBy parameter of the formatting cmdlets, such as Format-Table and Format-List, to group objects. Unlike Group-Object, which creates a single table with a row for each property value, the GroupBy parameters create a table for each property value with a row for each item that has the property value.

Group-Object doesn't require that the objects being grouped are of the same Microsoft .NET type. When grouping objects of different .NET types, Group-Object uses the following rules:

  • Same Property Names and Types.

    If the objects have a property with the specified name, and the property values have the same .NET type, the property values are grouped by the same rules that would be used for objects of the same type.

  • Same Property Names, Different Types.

    If the objects have a property with the specified name, but the property values have a different .NET type in different objects, Group-Object uses the .NET type of the first occurrence of the property as the .NET type for that property group. When an object has a property with a different type, the property value is converted to the type for that group. If the type conversion fails, the object isn't included in the group.

  • Missing Properties.

    Objects that don't have a specified property can't be grouped. Objects that aren't grouped appear in the final GroupInfo object output in a group named AutomationNull.Value.

The output groups are presented in order the group were created. The items belonging to each group are not sorted. They are listed in the order in which they were received.