Measure-Object in PowerShell

Measure-Object: Where-Object Versus -FilterMeasure-Object | Where-Object v -Filter

On this page we have a real-life task for Measure-Object.  We are going to compare the performance of PowerShell’s -Filter with Where-Object.  In a nutshell, we want to see if it speeds up a script to filter the data stream before piping it into a Where-Object statement.


Our Mission: Measure-Object

Our experiment is to see how fast PowerShell filters objects at source, compared with employing the Where-Object {construction}.

Stage 1: Preliminary Task to List the DLL Files

The task is to obtain a subset of files.  For this example we want to list all of the dynamic link library files in the Windows\System32 folder.

Method 1: -Filter

$WinDll = "C:\Windows\System32\"
Get-ChildItem -Path $WinDll -Filter "*.dll" -Recurse

Learning Points

Note 1: Get-ChildItem has a famous alias gci, which I use in later scripts.

Note 2: Some sub-folders have restrictions which cause errors, thus we could append this instruction: -ErrorAction SilentlyContinue (abbreviates to "ea 4" in Method 2).

Method 2: Where-Object

$WinDll = "C:\Windows\System32\"
Gci -Path $WinDll -Recurse |
Where-Object Extension -eq ".dll" -ErrorAction SilentlyContinue

Note 3: As an alternative to -Filter we can pipe the list of files into a Where-Object clause.

Stage 2: Use Measure-Object to Compare -Filter and Where-Object

This is the main event, to compare Method 1 with Method 2.

$WinDll = "C:\Windows\System32\"

# Measuring using Method 1: -Filter
$FiltSource = Measure-Command -Expression {
Gci -Path $WinDll -Filter *.dll -Recurse -ea 4}

# Measuring using Method 2: Where-Object
$WhereObj = Measure-Command -Expression {
Gci -Path $WinDll -Recurse -ea 4 | Where-Object Extension -eq ".dll"}

# Comparing Method 1 and 2
$Diff = [Math]::Truncate($WhereObj.TotalMilliseconds) –

#$Diff = $WhereObj – $FiltSource
"Where  takes " + [Math]::Truncate($WhereObj.TotalMilliseconds) + " ms"
"-Filter takes " + [Math]::Truncate($FiltSource.TotalMilliseconds) + " ms"
If($FiltSource -lt $WhereObj)
{Write-Host "`nUsing -Filter is $Diff ms faster than Where-Object."}
Else {Write-Host "Where-Object is $Diff ms faster than -Filter."}

Note 5: By default Measure-Object formats time as hours:minutes:seconds.milleseconds.  By combining [Math] and .TotalMilliseconds we are able to get a single number to compare with the other method.

Note 6: The backtick (`) can be tricky, this is why I ended the line with an opening bracket { or, a – (minus), PowerShell realises this syntax means the command continues on the next line.
Gci -Path $WinDll -Filter *.dll -Recurse -ea 4}
{Gci -Path $WinDll -Filter *.dll -Recurse -ea 4}

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Maths With Measure Object

I struggled to find the largest number in an array until I hit on this technique of using -Maximum.

$PrimeNum = @(2,3,5,7,11,13,17,19,23,29,31,37)
$Biggest = ($PrimeNum | Measure-Object -Maximum).Maximum

Note 7: You could use parameters for other math terms, for example -sum or the useful -average.


a) Check the Output

$PrimeNum = @(2,3,5,7,11,13,17,19,23,29,31,37)
$PrimeNum | Measure-Object -sum

Count : 12
Average :
Sum : 197
Maximum :
Minimum :
Property :

b) Further Research with Get-Help

Get-Help Measure-Object -full

Note 8: Thanks to -full, we see useful examples.

See More About Measure-Object

Measure-Object computes maths such as sum, average and maximum.  Thanks to the concept of an object, this cmdlet can also calculate the properties of text objects, such as the number of words in a document.

See more on PowerShell’s Measure Object

Summary of PowerShell Measure-Object

We use Measure-Object to compare two PowerShell techniques namely: Where-Object and -Filter.  The result shows that it is better to filter at source than employ the Where-Object clause.

(Get-WmiObject Win32_PhysicalMemory | Measure-Object Capacity -sum).sum/1mb

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


See more Microsoft PowerShell Examples of Real Life Tasks

PowerShell Real-life Examples   • Test-Connection   • Invoke-Expression   • Invoke-Command

Windows PowerShell   • Com   • PowerShell -Filter Where-Object   • PowerShell 3 Rename-Computer

PowerShell Registry   • Compare-Object Registry   • Measure-Object   • Measure-Command

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.