Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Group-Object
Groups objects that contain the same value for specified properties. In this articleSyntax
DescriptionThe If you specify more than one property, Beginning in PowerShell 7, ExamplesExample 1: Group files by extensionThis example recursively gets the files under This example uses the NoElement parameter to omit the members of the group.
Example 2: Group integers by odds and evensThis 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.
Example 3: Group hashtables by key valueBeginning in PowerShell 6, This example uses the NoElement parameter to omit the members of the group.
Example 4: Group event log events by EntryTypeThis 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.
Example 5: Group processes by priority classThis example demonstrates the effect of the NoElement parameter. These commands group the processes on the computer by priority class. The first command uses the 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.
Example 6: Group processes by nameThe following example uses
Example 7: Group objects in a hash tableThis 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 The second
command displays the hash table in The third command uses dot notation,
Example 8: Create a case-sensitive hash tableThis example combines the CaseSensitive and AsHashTable parameters to create a case-sensitive hash table. The files in the example have extensions of
The 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. Beginning in PowerShell 7, to create case-sensitive hash tables, include CaseSensitive and AsHashtable in your command.
-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. Beginning in PowerShell 7, to create case-sensitive hash tables, include CaseSensitive and AsHashtable in your command.
-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 To group the objects in a collection, pipe the objects to
-NoElementIndicates that this cmdlet omits the members of a group from the results.
-PropertySpecifies the properties for grouping. The objects are arranged into groups based on the value of the specified property. 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:
For more information, see about_Calculated_Properties.
InputsPSObject You can pipe any object to OutputsMicrosoft.PowerShell.Commands.GroupInfo or System.Collections.Hashtable When you use the AsHashTable parameter, NotesPowerShell includes the following aliases for
You can use the GroupBy parameter of the formatting cmdlets, such as
FeedbackSubmit and view feedback for When you make a copy of the aggregate object and of the objects?*There are more than two but the book covers two. When you make a copy of the aggregate object, it is important that you also make copies of the objects it references - this is known as a "deep copy".
When two object references refer to the same object the references are called?Object reference equality: when two object references point to the same object. Object value equality: when two separate objects happen to have the same values/state.
When an object is passed as an argument it is actually a reference to the object that is passed True False?When an object, such as a String , is passed as an argument, it is actually a reference to the object that is passed. The contents of a String object cannot be changed. When passing multiple arguments to a method, the order in which the arguments are passed is not important.
When an object is passed as an argument it is actually a reference?When an object is passed as an argument to a method, this is actually passed. this is the name of a reference variable that is always available to an instance method and refers to the object that is calling the method. This array field holds the number of elements that the array has.
|