Powershell

PowerShell count with Measure-Object

PowerShell is an open-source command-line tool with cross-platform availability. The PowerShell tool allows one to automate several tasks and perform few tedious tasks by executing commands. The Measure-Object is a PowerShell utility that acts as a cmdlet to count objects. The extended functionality (can be achieved by using parameters) of Measure-Object can be used to get the maximum, minimum, average, sum, and standard deviation of numeric values. Moreover, it can also be applied to strings to count a number of lines, words, or characters.

In this article, the PowerShell count with Measure-Object is explained in detail and several examples are provided for better understanding.

How count works with Measure-Object

The count is the default property of the Measure Object. The Measure-Object keyword can be piped with strings, variables, or objects to get the desired answer. Here we are targeting the count with Measure-Object. The Measure Object follows the syntax provided below:

>  <object> | Measure-Object

The upcoming sections provide usage of count with measure objects in multiple scenarios.

Count the number of entries

When PowerShell Cmdlets are used with Measure-Object, it returns the count of the entries. For instance, we are piping Measure-Object with the Get-Command cmdlet and the following command is executed in this regard.

Note: As the Get-Command cmdlet prints the output in four columns, CommandType, Name, Version, and Source. So, you can apply any of these properties with Measure-Object as well.

> Get-Command | Measure-Object

To get the services list, PowerShell supports the Get-Services cmdlet. One can use Measure-Object with the Get-Services cmdlet to count the number of services. We have experienced its working by using the command provided below. The output shows that 288 services are on board right now.

> Get-Service | Measure-Object

In the above two commands, Measure-Object has been used on several cmdlets individually.

Measure-Object can be used with multiple cmdlets. In order to demonstrate this, we carried out the following four commands:

> $comm = Get-Command
> $ser = Get-Service
> $Total = $comm + $ser
> $Total | Measure-Object

The first two commands store the Get-Command and Get-Service cmdlet in two variables named $comm and $ser respectively.

The third command stores the sum of $comm and $ser in a new variable named $Total.

And the last command pipes that $Total variable with Measure-Object.

The output shows that the entries of both cmdlets (Get-Command and Get-Service) are summed up.

Count number of files/directories

The Get-ChildItem cmdlet of PowerShell lists down the files and directories in the current folder. When the Measure-Object is executed with Get-ChildItem, it would return the total count of the files and directories as shown in the output of the command mentioned below.

> Get-ChildItem | Measure-Object

Determine how many characters, words, and lines there are

A count with Measure-Object can be used to count the number of characters, lines, and words in the file. For this, you have to use the Get-Content cmdlet on that file and then pipe it with the Measure-Object Cmdlet. The command written below gets the content of a text file located at “F:\” and then counts the number of Characters, Words, and Lines using Measure-Object.

> Get-Content "F:\contact.txt" | Measure-Object -Word -Character -Line

Apart from getting the content from a file, a string can directly be passed to count the characters/words/lines. The command provided here pipes a string with Measure-Object Cmdlet to count the characters, lines, and words in that string.

> "PowerShell is a cross-platform command line tool" | Measure-Object -Word -Character -Line

Count the number of input of hashtables and integers

Apart from counting the input of string fields. The Count with Measure-Object can count the number of inputs for hash tables and integer values as well. For instance, the following command gets four values as a hashtable and then these values are executed with Measure-Object. So, the output would be 4 because Measure-Object counts the number of inputs passed to it.

> @{val=5}, @{val=10}, @{val=15}, @{val=20} | Measure-Object

For integers, it acts the same. The following command gets integer values as an input and when these values are piped with Measure-Object, the output shows the number of inputs.

> 2, 4, 6, 8, 10, 12 | Measure-Object

In the output shown above, the Average, Sum, Maximum, Minimum, and Property options are empty as we have not passed them with Measure-Object. If you want to get these values too, you have to use the -AllStats option with Measure-Object as described below:

> 2, 4, 6, 8, 10, 12 | Measure-Object -AllStats

Conclusion

PowerShell supports multiple cmdlets to perform the tasks automatically. The Measure-Object is one of them and it counts the number of entries in any object, cmdlet, function, and many more. This article explains the working of count with Measure-Object in PowerShell. Several examples are also provided that show the usage of multiple perspectives. Apart from this guide, you can visit linuxhint for more PowerShell tutorials. Happy Computing !!

About the author

Adnan Shabbir