Windows PowerShell


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 parmeter, which is an alternative technique to the group-Object 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.

Group-Object Topics

 ♣

Example 1 Process

get-Process | group-Object company
# Compared with:
get-Process | group-Object company | sort-Object count -descending

While I favour the full command (above), you can omit '-object' from group-Object.  This shortened version (below) will work:
get-process | group company | sort count -descending.

Here is a parallel technique, which achieves a slightly different result by using format-Table and its parameter -groupby:
get-Process | sort-Object company | format-Table name, company -groupby company -auto

Example 2 Service - Sorting on Two Criteria

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.

get-Service | sort-Object status, name | format-Table -groupby status
#Compared with
get-Service | sort status, name | ft -groupby status Name, DisplayName, Status -auto

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

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

get-Eventlog system -newest 3000 |group-Object eventid | `
sort-Object count -descending |format-Table count, name -autosize.

Example 3b

get-Eventlog system -newest 3000 |sort-Object eventid | `
where {$_.EntryType -eq "Error"} |format-Table -groupby eventid, EntryType, Message.

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

Further Research on group-Object

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:

get-Help group-Object -full

To be frank, for once help does not offer much more information, however, this in itself gives peace of mind that there is no more to know about this cmdlet.

Another example of -groupby

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teaches me more about how the system literally operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

Summary 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.

See more PowerShell examples for syntax

PowerShell Home  • Syntax  • ISE (GUI)  • Pipeline  • Format-table  • Group  • Sort

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

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.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.