Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 34 ยท Objects and the Pipeline

Group-Object, Measure-Object and ForEach-Object

Summarize objects in PowerShell: group and count with Group-Object, total and average with Measure-Object, and run code per object with ForEach-Object.

Once you can filter, select and sort, the next question is usually "how many?" or "how much?". How many errors per source? How many bytes per file type? What is the average size? PowerShell answers with two summarizing cmdlets, Group-Object and Measure-Object, and one general-purpose workhorse, ForEach-Object, which runs your own code on every object.

These three cover most of what a spreadsheet pivot table or a SQL GROUP BY would do in a shell session. This lesson shows each one, then explains the begin, process and end phases of ForEach-Object that also appear in advanced functions.

Real PowerShell cannot run on this page, so the PowerShell lines are read-only. The runnable examples are a Python stand-in that builds the same summaries with dictionaries and itertools.groupby. Python is only the model here: it shows what the cmdlets compute, not how PowerShell runs them.

Summarize in three tools

Group-Object Bucket objects by a property and count them.
Measure-Object Count, sum, average, minimum, maximum.
ForEach-Object Run a script block for every object.

Group to split, measure to total, ForEach to do your own thing.

1 / 2

Group-Object: split into buckets

Group-Object collects objects that share a value into groups. Each output object has three properties: Name (the shared value), Count (how many objects) and Group (the original objects).

POWERSHELL
Get-ChildItem C:\Logs | Group-Object -Property Extension

Typical output (the exact columns depend on your system):

TEXT
Count Name                      Group
----- ----                      -----
   12 .log                      {a.log, b.log, c.log, d.log...}
    3 .txt                      {notes.txt, todo.txt, readme.txt}
    1 .csv                      {export.csv}

A few things to know:

  • Groups appear in order of first appearance, not sorted. Add Sort-Object Count -Descending to rank them.
  • Comparison is case-insensitive by default, so Error and ERROR land in the same group. Use -CaseSensitive to separate them.
  • -NoElement leaves out the Group property when you only need counts. That saves memory on big inputs.
  • Several properties make a combined key: Group-Object Status, StartType gives names like Running, Automatic.
  • -AsHashTable returns a hashtable keyed by the group value, which is a fast way to look up whole groups later (add -AsString when the keys are not strings).
POWERSHELL
Get-Service | Group-Object Status -NoElement | Sort-Object Count -Descending

To get at the members of one group, use the Group property:

POWERSHELL
$groups = Get-ChildItem C:\Logs | Group-Object Extension
($groups | Where-Object Name -eq '.log').Group | Select-Object -First 3

You can also group by a computed key with a script block: Group-Object { $_.Length -gt 1MB } splits the files into "True" and "False" groups.

Measure-Object: count, total and average

On its own, Measure-Object only counts the objects it receives:

POWERSHELL
Get-ChildItem C:\Logs | Measure-Object

The output has a Count property. To do arithmetic on a property, name it and switch on the statistics you want:

POWERSHELL
Get-ChildItem C:\Logs -File |
    Measure-Object -Property Length -Sum -Average -Minimum -Maximum

The result object has Count, Sum, Average, Minimum, Maximum and the property name it measured. Only the statistics you ask for are filled in, the others show as empty. The values are plain numbers, so get one with .Sum or Select-Object -ExpandProperty Sum:

POWERSHELL
$total = (Get-ChildItem C:\Logs -File | Measure-Object Length -Sum).Sum
"{0:N0} bytes in total" -f $total

Measure-Object can also analyze text: Get-Content notes.txt | Measure-Object -Line -Word -Character counts lines, words and characters, much like the Unix wc command.

Measure-Object ignores objects that lack the property you named instead of failing, which is convenient but can hide typos. If a result looks too small, check the property name with Get-Member.

Also note that .Count on a collection you already hold in a variable ($files.Count) is much faster than piping to Measure-Object when you only need the number.

ForEach-Object: run your own code per object

ForEach-Object takes a script block and runs it once per input object, with $_ (or $PSItem, the same thing) set to the current object. Whatever the block outputs goes down the pipeline.

POWERSHELL
1..5 | ForEach-Object { $_ * $_ }
Get-ChildItem *.log | ForEach-Object { "{0} is {1} KB" -f $_.Name, [math]::Round($_.Length / 1KB, 1) }

When you only want one property or one method, there is a shorter form that takes the member name:

POWERSHELL
Get-Process | ForEach-Object Name
Get-ChildItem | ForEach-Object ToString

begin, process and end

A ForEach-Object has three blocks. Begin runs once before the first object, Process runs once per object, and End runs once after the last. Use them for set-up, per-item work and a final summary:

POWERSHELL
1..4 | ForEach-Object -Begin   { $sum = 0 } `
                      -Process { $sum += $_ } `
                      -End     { "Total: $sum" }

Inside -Begin and -End, $_ is not set, because there is no current object. This is the same begin, process, end structure you will use in Advanced Functions.

Pitfalls and parallelism

  • foreach the statement versus ForEach-Object the cmdlet. foreach ($x in $list) { } is a language statement (see Conditionals and Loops); ForEach-Object is a pipeline cmdlet. The alias foreach in the middle of a pipeline means the cmdlet, which is a classic source of confusion.
  • Do not use ForEach-Object just to read a property. Select-Object -ExpandProperty or plain member access ($items.Name) is clearer.
  • In PowerShell 7, ForEach-Object -Parallel { ... } runs blocks on several threads. Outside variables are reached with the $using: prefix, and -ThrottleLimit caps how many run at once. Windows PowerShell 5.1 does not have -Parallel. See Remoting, Jobs and Parallelism.

Stand-in: group, measure and loop in Python

The data below is a small, made-up directory listing. First, a Group-Object -NoElement | Sort-Object Count -Descending equivalent, then Measure-Object statistics, then the begin, process, end pattern. Python's itertools.groupby only groups neighbouring items, so the code sorts first, which is a difference from Group-Object, which collects equal keys wherever they appear.

Python Group by extension and measure sizes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

old.LOG ended up in the .log group because the key was lowercased first, which is what PowerShell's default case-insensitive grouping does for you. With -CaseSensitive, .LOG would form its own group.

The second example models ForEach-Object -Begin, -Process and -End with a small function. The three callables stand in for the script blocks, and the "pipeline" is a list.

Python begin, process and end

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first run prints a list with one item, the total, because process produced no output of its own. This matches PowerShell: a script block only sends down the pipeline what it outputs. In real PowerShell, $sum += $_ is an assignment and does not output anything either.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the three properties of an object produced by Group-Object?

    Group-Object returns objects with Name (the shared value), Count (how many) and Group (the original objects). -NoElement leaves out Group.
  2. 2. Which command gives the total size of the files in the current folder?

    Measure-Object computes statistics on a named property. -Sum asks for the total; the result is in the Sum property of the output object.
  3. 3. Inside the -Begin block of ForEach-Object, what is $_ ?

    Begin runs once before any input is processed, and End after all of it. Only the Process block has a current object.
  4. 4. Group-Object Status is run on services where some say Running and others RUNNING. What happens by default?

    Group-Object ignores case unless you add -CaseSensitive, which is why Error and ERROR group together.

Summary

  • Group-Object buckets objects by one or more properties (or a script block) and returns Name, Count and Group; -NoElement drops Group, and results appear in order of first appearance.
  • Measure-Object counts by default; -Property with -Sum, -Average, -Minimum and -Maximum does arithmetic, and -Line, -Word, -Character analyze text.
  • ForEach-Object runs a script block per object, with $_ or $PSItem as the current object, and it accepts -Begin, -Process and -End.
  • A member name can replace the script block, and PowerShell 7 adds -Parallel with $using:.
  • The foreach statement and the ForEach-Object cmdlet share a name but are different things.

Next, learn how to present results in Formatting and Output.