Windows PowerShell


Scripting  PowerShell Group-Object

Introduction to Scripting PowerShell Group-Object

Many GUIs 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 -groupby, which is an alternative technique to group-Object.

Once we have grouped objects, it often adds clarity if we add 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
get-Process | group-Object company | sort-Object count -descending

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

Here is a parallel technique, which achieves the same result but using format-Table and -groupby:
get-Process | sort-Object company | format-Table -groupby company

Example 2 Service

get-Service | group-Object status
get-Service | group-Object status | format-list

Again, here is an alternative technique, observe how format-Table with -groupby can enhance the output.  With format you can refine the output by specifying the properties. Format-table's, sister command is format-list.

get-Service | sort-Object status | format-Table -groupby status
get-Service | sort-Object status | format-Table -groupby status Name, DisplayName, Status

Example 3 Eventlog

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

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

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

Another example of -groupby

  ˚

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 Microsoft PowerShell syntax

PowerShell Home  • Syntax  • -f format  • Pipeline  • Quotes  • Format-table  • Group  • Select-String

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

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

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

Please report a broken link, or an error.