Measure objects in PowerShell with the Measure-Object cmdlet

According to the help content, Measure-Object 'calculates the numeric properties of objects, and the characters, words, and lines in string objects, such as files of text.' This is a vague description of what this command can do, so let's dive into that and go over a few contexts where using the Measure-Object command might make sense.

Peter Drucker, a legendary management consultant, is quoted as saying, “You can't manage what you can't measure." This quote deals with managing work processes from a company-management perspective but nonetheless applies to lots of activities.

In our IT pro world, we come across problems that require all kinds of measurement every day. Perhaps one day it's building a summary report from a CSV file, checking how many servers we successfully patched the night before, or how many widgets our company sold last week.

There are lots of free and paid tools out there that allow you to perform all kinds of measurements and calculations. But nearly all are overkill if you just want to check quickly how many tables are in a database. For these simple examples and times when you need to perform any kind of calculation in your PowerShell scripts, you've got the Measure-Object command available.

Measure-Object can provide five different numbers given a set of data: Count, Average, Sum, Minimum, and Maximum. These labels are self-explanatory, but how to feed data into Measure-Object isn't necessarily so.

Measure-Object accepts pipeline input, and this is where you'll see this command used in the more frequent contexts. The values passed to it determine how it calculates its figures.

For example, let's just take a simple case like a string or an integer and pipe that to Measure-Object.

Measure-Object simple case

Measure-Object simple case

Notice that it doesn't care what kind of input it gets. It's just counting the number of objects passed to it and displays the total count. We could generate ten different objects at once in an array and pass those to it, and we'd get the same result.

While we're here, we can also populate the other values Measure-Object can calculate by specifying them as parameters. Note that we can only gather these numbers from integers. Using strings in this case will result in an error.

How about reading a CSV file of employees and how many widgets they built?

We try to get the average number each worker built but immediately run into a problem.

The CSV has multiple properties, and Measure-Object doesn't know which one to use. For this instance, we need to use the Property parameter to tell Measure-Command we'd like to average the WidgetsBuilt column.

Measure-Object can receive any set of data and provide average, sum, maximum, and minimum values for numbers and a count value for all types of objects.

For one last example, Measure-Object is also great at calculating numbers involving files as well. Below I know that my C:\Windows folder has 28 files with the biggest one being around 268 kB.

It doesn't matter what the data set is. As long as each object has an integer property attached to it, Measure-Object can summarize any set of data you throw at it.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!


Leave a reply

Your email address will not be published. Required fields are marked *


© 4sysops 2006 - 2020


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account