Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



Scripting PowerShell Group-Object

Introduction to Scripting PowerShell Group-Object

GUIs such as Task Manager lack the ability to group columns or objects.  Hence, one of the benefits of using a PowerShell script is that you can append a Group-Object clause, and thus get a more meaningful display of data.  I have also included the Format-Table -groupby parameter, which is an alternative technique to the Group-Object cmdlet.

PowerShell Group-Object Topics

 ♣

Example 1 Group Processes

# PowerShell Group Example
Clear-Host
Get-Process | Group-Object company
# Compared with:
Get-Process | Group-Object company | Sort count -descending

While I favour the full command (above), you can omit '-object' from Group-Object (just as omitted it from Sort-Object.  This shortened version (below) will work:

Get-Process | group company | sort count -descending.

-GroupBy Alternative to Group-Object

Here is a parallel technique, which achieves a slightly different result by using Format-Table and its parameter -groupby:

# PowerShell -groupBy Example
Clear-Host
Get-Process | Sort-Object company | `
Format-Table name, company -groupby company -auto

Note 1: The backtick (`) causes the command to word-wrap to the next line.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI Monitor

Example 2 Service - Sorting on Two Criteria

This Group-Object example employs Get-Service as a vehicle for testing this PowerShell cmdlet.

Once we have grouped objects, we can add clarity by appending extra code which sorts the items into numeric or alphabetical order.  Observe in the following examples how PowerShell provides Sort-Object for sequencing the output.

# Preliminary Group Example
Clear-Host
Get-Service | Group-Object status
#Compared with
Get-Service | Group-Object status | Format-List

Again, here is an alternative technique employing Format-Table with -groupby to enhance the output.  With Format-Table you can refine the output by specifying the properties.

# Sort and Group on Two Criteria
Get-Service | Sort-Object status, name `
| Format-Table -groupby status

 

# Compared with
Clear-Host
Get-Service | sort status, name `
| Ft -groupby status Name, DisplayName, Status -auto

Note 2:  These examples sort firstly on 'status', then secondly on 'name'.

Note 3:  Ft is an alias for Format-Table.

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Example 3 Eventlog

With Get-Eventlog, always remember the name of the log!  System, Application, Security or which ever log you are investigating.

Example 3a  Group-Object

# PowerShell Group-Object and Sort
Clear-Host
Get-Eventlog system -newest 3000 |Group-Object eventid `
| Sort-Object count -descending |Format-Table count, name -autosize

Example 3b -groupBy

# PowerShell Sort-Object then -groupBy
Clear-Host
Get-Eventlog system -newest 3000 |Sort-Object eventid `
| where {$_.EntryType -eq "Error"} | Format-Table -groupby eventid

Note 4:   Observe the backtick (`), this is like word-wrap, it tells PowerShell that the command continues on the next line.

Another example of -groupby

Research Group-Object Parameters

Sometimes we get carried away and forget the basics.  In the case of PowerShell this means remembering 'help' with the -full parameter.  Thus in this instance we need:

# Group-Object Parameters
Clear-Host
Get-Help Group-Object -full

Just when I thought this PowerShell cmdlet has no interesting parameters, I discovered Group-Object's -AsHashTable, see more details here.

Summary of PowerShell's Group-Object

Group-Object is a useful addition to your PowerShell tool-kit, indeed the ability to control data output is a one reason for employing PowerShell rather than using the GUIs. A typical scenario for Group-Object is where you wish to aggregate the data displayed by a PowerShell query. As usual, you are spoilt for choice, the decision lies between piping to Group-Object, or alternatively to experiment with Format-Table -groupby.

If you like this page then please share it with your friends

 


See more PowerShell examples for syntax

PowerShell Home  • Syntax  • ISE (GUI)  • Pipeline  • Format-Table  • Select-Object

Compare-Object  • Compare-Object (Registry)  • Group-Object  • Sort-Object  • Foreach

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: