Powershell

Understanding the PowerShell Group Object Command

The cmdlet “Group-Object” in PowerShell groups the objects for the specified property that contains the same value. Each property value is returned in a row, accompanied by a column with multiple items corresponding to that value. A property group is grouped by its first value if more than one property is specified. After that, it groups the properties based on the value of next property.

This article will provide a detailed guide about the cmdlet “Group-Object”.

Understanding the PowerShell Group Object Command

As stated above, the “Group-Object” cmdlet in PowerShell is used to group the object based on their types, or extensions. Examples demonstrating the concept are provided below.

Example 1: Group the Files by Their Extension Using the “Group-Object” Cmdlet

The following example will illustrate to get and group the files by their extension:

$files_path = Get-ChildItem -Path C:\Doc\ -Recurse
$files_path | Group-Object -Property extension -NoElement | Sort-Object -Property Count -descending

 

According to the above code snippet:

  • First, initialize a variable and assign the “Get-ChildItem” cmdlet along with the “-Path” parameter.
  • After that, assign the path and add the “-Recurse” parameter at the end:

Example 2: Group the Integers on the Base of Odd and Even Numbers

This demonstration will get the even or odd numbers from the given integers:

$numbers = 1,2,3,4,5,6,7,8
$numbers | Group-Object -Property {$_ % 2}

 

According to the stated code above:

  • First, initialize a variable and then assign the integers from “1” to “8” separated by commas.
  • Then, specify the integer assigned variable followed by the “|” pipeline.
  • After that, add the “Group-Object” cmdlet along with the “-Property” parameter.
  • Lastly, assign the “{$_ % 2}” to the “-Property” parameter to get the remainder, such as even or odd:

Example 3: Group the Processes by Name Using the “Group-Object” Cmdlet

The following example will get and group the processes by name:

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

 

In the above code snippet:

  • First, write the cmdlet “Get-Process” followed by the “|” pipeline.
  • Then, add the “Group-Object” cmdlet along with the “-Property” parameter and assign the value “Name” to it.
  • After that, add another parameter “-NoElement” and again add the “|” pipeline.
  • Lastly, specify the “Where-Object” cmdlet and define the condition “{$_.Count -gt 1}”:

That was all about understanding the PowerShell “Group-Object” cmdlet.

Conclusion

The cmdlet “Group-Object” in PowerShell groups the items based on their types such as names, or extensions. It displays the results in a table format. The above post has elaborated on the “Group-Object” cmdlet with its examples.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.