PowerShell Measure-Object Cmdlet

PowerShell Measure-Object

Measure is one of PowerShell's more interesting and unusual verbs; it deals with 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.

Topics for Measure Object


Example 1: PowerShell Measure-Object

Processes running on Windows operating systems have a base priority ranging from 0 to 13.  Here is a simple example using Measure-Object to check maximum and minimum values of processes.

# PowerShell Measure-Object
Get-Process | Measure-Object BasePriority -max -min

Learning Points

Note 1: Take the time to understand the rhythm of this command.  Obtain object (Get-Process), then | (pipe) the output to Measure-Object; only then do you specify the property (BasePriority) to extract the numeric data.

Example 1a: Measure-Object -Average

I admit that this example has little practical application, but stick with me and learn just a little more about how you can employ Measure-Object

# PowerShell Measure-Object -Average
$Avg = (Get-Process | Measure-Object BasePriority -average)
"BasePriority average = " + [System.Math]::Round($Avg.average)

Note 2: I just wanted to explain how useful [System.Math] is with Measure-Object.

Note 3: For newbies constructions such as ($Avg.average) may seem a little strange, especially as we have already specified -average in the line above.

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

Example 2:  Measure-Object with Test-Connection

This example has a real-life purpose, namely to test the speed of your connection to google.com.  If you have no internet connection change the value of $Site to another computer on your network.

# PowerShell Measure-Object to test a website's response time
Write-Host `n "Waiting for test ..."
$Avg = 0
$Site = "www.google.com"
$PingSite = Test-Connection -count 5 $Site
$Avg = ($PingSite | Measure-Object ResponseTime -average)
$Calc = [System.Math]::Round($Avg.average)
Write-Host "Average response time to $Site is $Calc ms"

Note 4: To truly understand this example spend a little time working with Test-Connection.  Researching with Get-Member will show that the crucial property of ResponseTime comes from Test-Connection and not from Measure-Object.

See more on PowerShell's Test-Connection »

Example 3:  Measure-Object Counting Files

There various methods of employing PowerShell to count the files in a folder; here is one using Measure-Object.

# Measure-Object counts Windows files
Get-ChildItem C:\Windows | Measure-Object

If you ask Get-Help to check Measure-Object's parameters then you can see how knew the example below would return useful values.

# PowerShell Measure-Object with text
Get-content C:\Windows\System.ini -force |
Measure-Object -line -character -word

Note 5: In true PowerShell form, each of the parameters, -line, -character -Is a singular word.

Example 4:  Measure-Object Counts File Types

This example employs Measure-Object to count the number of .exe files in the Program Files.  You may deduce that I used a modified version to check the number of graphics files in my pictures folder.  For you to that to work on your machine, first you need to change the value of $Path to the location of your picture files.

$Path = "C:\Program Files\"
$Files = Get-ChildItem -recurse $Path -Include *.jpg,*.gif,*.png,*.exe
$Files | Group-Object {$_.extension.substring(1)} |
Foreach {
$HowMany =  $_.group | Measure-Object
Write-Host "Number of $($_.name): $($_.count)"

Note 6: The first part of the example is concerned with getting a stream of file types grouped by extension.  -recurse and -Include can be tricky if you have not used them before.

Note 7: This example is ripe for experimentation.  For instance, you could tinker with the 'Write-Host' instruction.  Also you could change $_.name to $HowMany.name if that make its workings clearer.

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download Solarwinds Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click and import the users.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive application analysis software,
Download a free trial of SAM (Server & Application Monitor)

Example 5:  Measure-Object Sums File Size

The scenario: You wish to calculate the size occupied by all the files in a folder. Naturally, you could add the -Recurse parameter if you wanted to add the files in subdirectories.

Observe how this example makes use of -Property and -Sum

# Measure-Object calculates the file size of System32 folder
$Pathy = "C:\Windows\System32"
$Sizey = (Get-ChildItem $Pathy | Measure-Object -Property length -Sum)
[math]::floor($Sizey.Sum /1MB)

See another example of Measure-Object ยป

Measure-Object Math

Here are examples of controlling the decimal places with the Round() function.  If it's whole numbers you want then consider appending -as [int].

# Measure-Object calculates the file size of System32 folder
$Pathy = "C:\Windows\System32"
$Sizey = (Get-ChildItem $Pathy | Measure-Object -Property length -Sum)
[System.Math]::Round(($Sizey.Sum /1MB),2)

# ($Sizey.Sum /1MB) -as [int]

Background Research Into Measure-Object

Get help with the parameters

# Research Measure-Object's parameters
Help Measure-Object -full

Note 8: Observe useful parameters such as -Sum and -Property.

Check Measure-Object with Get-Member

# Research Measure-Object's properties
Measure-Object | Get-Member

Note 9: Checking with Get-Member will reveal which math properties are available, and which are not allowed.

Researching Similar PowerShell Cmdlets

# PowerShell Measure Cmdlet Research
Get-Command -verb Measure

Note 10: This reveals one sister command Measure-Command ».

Summary of PowerShell Measure-Object

Measure-Object is often an alternative to other techniques for obtaining statistics about various objects that you are dealing with.  Take the trouble to see how its basic math switches work.

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



Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2017 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: