The Group-Object command

The Group-Object command shows a group and count for each occurrence of a value in a collection of objects.

Given the sequence of numbers shown, Group-Object creates a Name that holds the value it is grouping, a Count as the number of occurrences of that value, and a Group as the set of similar values:

PS> 6, 7, 7, 8, 8, 8 | Group-Object

Count Name Group
----- ---- -----
1 6 {6}
2 7 {7, 7}
3 8 {8, 8, 8}

The Group property may be removed using the NoElement parameter, which simplifies the output from the command:

PS> 6, 7, 7, 8, 8, 8 | Group-Object -NoElement

Count Name
----- ----
1 6
2 7
3 8

Group-Object can group based on a specific property. For example, it might be desirable to list the number of occurrences of particular files in an extensive folder structure. In the following example, the C:WindowsAssembly folder contains different versions of DLLs for different versions of packages, including the .NET Framework:

Get-ChildItem C:WindowsAssembly -Filter *.dll -Recurse | 
    Group-Object Name 

Combining Group-Object with commands such as Where-Object and Sort-Object allows reports about the content of a set of data to be generated extremely quickly, for example, the top five files that appear more than once in a file tree:

PS> Get-ChildItem C:WindowsAssembly -Filter *.dll -Recurse |
>> Group-Object Name -NoElement |
>> Where-Object Count -gt 1 |
>> Sort-Object Count, Name -Descending |
>> Select-Object Name, Count -First 5

Name Count
---- -----
Microsoft.Web.Diagnostics.resources.dll 14
Microsoft.Web.Deployment.resources.dll 14
Microsoft.Web.Deployment.PowerShell.resources.dll 14
Microsoft.Web.Delegation.resources.dll 14
Microsoft.Web.PlatformInstaller.resources.dll 13

As was seen with Sort-Object, Group-Object can group on more than one property. For example, we might group on both a filename and the size of a file (the Length property of a file):

PS> Get-ChildItem C:WindowsAssembly -Filter *.dll -Recurse |
>> Group-Object Name, Length -NoElement |
>> Where-Object Count -gt 1 |
>> Sort-Object Name -Descending |
>> Select-Object Name, Count -First 6

Name Count
---- -----
WindowsBase.ni.dll, 4970496 2
System.Xml.ni.dll, 6968320 2
System.Windows.Interactivity.ni.dll, 121856 2
System.Windows.Forms.ni.dll, 17390080 2
System.Web.ni.dll, 16481792 2
System.Web.ni.dll, 13605888 2

In the preceding example, we can see that System.Web.ni.dll appears four times (a count of 2, twice) in the folder structure, and that each pair of files has the same size.

Like Sort-Object, Group-Object is not limited to properties that already exist. It can create calculated properties in much the same way. For example, grouping on an email domain in a list of email addresses might be useful:

PS> '[email protected]', '[email protected]', '[email protected]' |
>> Group-Object { ($_ -split '@')[1] }

Count Name Group
----- ---- -----
2 one.example {[email protected], [email protected]}
1 two.example {[email protected]}

In this example, the split operator is used to split on the @ character; everything to the left is stored in index 0, while everything to the right is stored in index 1.

By default, Group-Object returns the collection of objects shown in each of the preceding examples. Group-Object is also able to return a hashtable using the AsHashtable parameter.

When using the AsHashTable parameter, the AsString parameter is normally used. The AsString parameter forces the key for each entry in the hashtable to be a string, for example:

PS> $hashtable = 'one', 'two', 'two' | Group-Object -AsHashtable -AsString
PS> $hashtable['one']

one

By default, Group-Object is case insensitive. The strings one, ONE, and One are all considered equal. The -CaseSensitive parameter forces Group-Object to differentiate between items where cases differ:

PS> 'one', 'ONE', 'One' | Group-Object -CaseSensitive

Count Name Group
----- ---- -----
1 one {one}
1 ONE {ONE}
1 One {One}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.88.130