top of page

PowerShell Grouping

Grouping Objects `Group-Object`

The Group-Object cmdlet is central to PowerShell's grouping capabilities. It allows you to aggregate objects based on one or more properties, enabling you to analyze data sets and perform operations on grouped data.

​

​# Grouping processes by CompanyName and State properties
Get-Process | Group-Object -Property @{Expression="CompanyName"; Expression="State"}

 

  • This command groups processes by their Handles property. Each group represents processes with the same number of handles. The output includes counts and details of processes in each group.
     

Grouping by Multiple Properties

# Grouping files by extension and displaying group information
Get-ChildItem -Path 'C:\Files\' | Group-Object -Property Extension | Select-Object Name, Count, @{Name="Files"; Expression={$_.Group}}

 

  • You can group objects by multiple properties using a hashtable (@{Expression="Property1"; Expression="Property2"}).

  • This example groups processes based on both CompanyName and State properties, providing a hierarchical grouping.

Customizing Grouping Output

# Grouping files by extension and displaying group information
Get-ChildItem -Path 'C:\Files\' | Group-Object -Property Extension | Select-Object Name, Count, @{Name="Files"; Expression={$_.Group}}

 

  • Use Select-Object to customize the output. This example groups files by their extension and selects properties like Name (extension), Count (number of files in each group), and Files (the actual file objects in each group).

Sorting Groups

# Sorting groups by count in descending order
Get-Service | Group-Object -Property Status | Sort-Object -Property Count -Descending

 

  • Sort groups using Sort-Object. Here, services are grouped by their Status and then sorted by the number of services in each group (Count) in descending order.

Filtering Groups

# Filtering processes grouped by WorkingSet greater than 100MB
Get-Process | Group-Object -Property @{Expression={$_.WorkingSet -gt 100MB}} | Where-Object {$_.Name -eq "True"}

 

  • Use Where-Object to filter groups based on specific criteria.

  • This example filters processes grouped by whether their WorkingSet is greater than 100MB (True groups).

Performing Calculations within Groups

# Calculating average CPU usage for each process name
Get-Process | Group-Object -Property ProcessName | ForEach-Object {
    [PSCustomObject]@{
        ProcessName = $_.Name
        AverageCPU = ($_.Group | Measure-Object -Property CPU -Average).Average
    }
}

​

  • Utilize ForEach-Object to perform calculations within each group. Here, processes are grouped by ProcessName, and the average CPU usage (CPU) is calculated for each process name using Measure-Object.

Conclusion

PowerShell's Group-Object cmdlet provides powerful capabilities for grouping and analyzing data in various scenarios. Whether you need to aggregate, filter, or perform calculations on grouped data, PowerShell offers flexible and efficient tools to manipulate structured information effectively. By mastering these grouping techniques, you can enhance your PowerShell scripts for tasks such as data reporting, system administration, and automation.

bottom of page