Skip to content
shellmap

measure-objectCount items and compute sum / min / max / average — like wc across all 5 shells

Equivalents in every shell

Bashunix
wc -l file.txt

`wc` counts bytes/words/lines/chars (`-c -w -l -m`) of input. For arithmetic over a column (sum, avg, min, max) the bash idiom is `awk '{s+=$1} END {print s, s/NR}' file.txt`. There is no single bash command that does both counting AND aggregation — `wc` + `awk` together cover `Measure-Object`.

Zshunix
wc -l file.txt

Same `wc` + `awk` toolchain. Zsh `$#array` gives length of an array variable without forking `wc`; `((sum = $array[1] + $array[2] + ...))` for in-shell arithmetic when the data is already a variable.

Fishunix
wc -l file.txt

Same external `wc` / `awk`. Fish's `count` builtin counts arguments (`count $list` = `$#list` in bash) — handy for in-shell collections, but for file content `wc` remains the tool.

PowerShellwindows
Get-Content file.txt | Measure-Object -Line -Word -Character

PowerShell-native cmdlet. `-Line -Word -Character` mirrors `wc -l -w -c`. `-Property <name> -Sum -Average -Minimum -Maximum -StandardDeviation` does single-pass column arithmetic. Output is one `GenericMeasureInfo` object with `Count`, `Sum`, `Average`, `Minimum`, `Maximum` properties — easy to project into a report.

cmd.exewindows
find /v /c "" file.txt

`find /v /c ""` counts lines (`/v` inverts the empty-pattern match; `/c` counts only). No native byte/word/character split; no arithmetic. For real measurement install Git Bash / WSL or shell out to PowerShell.

Worked examples

Count lines, words, and characters in a file

Bash
wc -lwm file.txt
PowerShell
Get-Content file.txt | Measure-Object -Line -Word -Character

Total disk usage of all .log files in the current directory

Bash
du -cb *.log | tail -n1
PowerShell
Get-ChildItem -File -Filter *.log | Measure-Object -Property Length -Sum

Average CPU time across all running processes

Bash
ps -eo pcpu | awk 'NR>1 {s+=$1; n++} END {print s/n}'
PowerShell
Get-Process | Measure-Object -Property CPU -Average

Gotchas

  • `Measure-Object` without any switches just COUNTS — equivalent to `wc -l`-on-objects. To get sum/avg/min/max you MUST pass `-Property <name>` AND at least one aggregate switch (`-Sum`, `-Average`, etc.). A common trap is `Get-Process | Measure-Object CPU` which silently returns `Count` only (treating `CPU` as a positional `-Property` argument with no aggregate requested).
  • On NON-numeric properties, `-Sum` / `-Average` THROWS. Strings only support `-Line`, `-Word`, `-Character` (and `-Maximum` / `-Minimum` in PS 6+ where lex sort is well-defined). For typed numeric fields imported from CSV, cast first: `Import-Csv data.csv | Measure-Object @{Expression={[double]$_.Price}} -Sum` (using a calculated property in PS 6+) or pre-project with `Select-Object`.
  • `-Line` / `-Word` / `-Character` are TEXT-mode switches: input must be strings. `Get-Process | Measure-Object -Line` returns the count of pipeline objects, not the line count of their `.ToString()` rendering. Pipe `Get-Process | Out-String -Stream | Measure-Object -Line` if you want lines of the formatted output.
  • `-Sum`, `-Average`, etc. accumulate using `[double]` — large integer sums can lose precision once they exceed `2^53`. For exact integer aggregates over big collections, fall back to `.Sum()` on `[long[]]` or accumulate in a `[bigint]` via `ForEach-Object`.
  • Empty input: `@() | Measure-Object -Property Anything -Sum` returns a `GenericMeasureInfo` with `Sum = $null` (NOT `0`). Downstream arithmetic on `$null + 5` returns `5` in PowerShell — usually fine, but `-eq 0` checks FAIL. Coalesce explicitly: `($result.Sum ?? 0)` in PS 7+, or `if ($null -eq $result.Sum) { 0 } else { $result.Sum }` on 5.1.

WSL & PowerShell Core notes

pwsh`Measure-Object` is identical on every pwsh platform — pure object math against the .NET runtime, no OS dependencies. There is no `measure` alias by default on any platform, so cross-platform scripts can spell the cmdlet name without conflict concerns.

Common tasks using measure-object

Related commands