Jul 13, 2016
In my recent post “PowerShell Basics – Measuring Objects (Part 1)” I talked about the Measure-Object cmdlet and how useful it was in finding the Count, Sum, Minimum and Maximum values of an array etc. Good stuff, but the Measure-Object cmdlet is also great for counting words, lines and characters etc. in text files, in fact it can be used to count any specified numerically formatted property of an object.
Technet has a fine article titled Using the Measure-Object Cmdlet where they use the Import-CSV cmdlet to import the contents of a simple text file formatted with 2 CSV fields, Name and Score, and then uses the Measure-Object cmdlet to give the usual average, sum etc. It even shows you how you can incorporate the Sort-Object and Select-Object cmdlets to display the top 5 names and score results.
All good stuff, but let's take that a little bit further; here I have used the same text file Technet lists in the above article, but now I am using the Measure-Object –Character, -Line and –Word parameters. In the following 2 examples I have specified first the Name and then the Score properties to see the different results. Note that the –Property is a positional parameter, so as a shortcut you could just specify the value alone as I have done in the 2nd example.
Of course, if you just want to do your sums on a simple plain text file, we could use the Get-Content cmdlet and pipe that into our Measure-Object cmdlet, and skip the property value. For example check out these additional parameters for yourself, and notice that curiously no matter what order they appear in your cmdlet, the default display is always Lines, Words, Characters and Property.
O.K, so that is all well and good, but the output of the Measure-Object cmdlet is still another object - what if all you wanted was just the count value itself, without all the other outputs, so you can use that count value in a later cmdlet or expression?
Well we can use a bit of lateral thinking here, and instead of using the Measure-Object cmdlet we will utilise a neat trick using array properties. Lets look at the following example:
Here I have used PowerShell ISE just to keep things clear. Line 1 is straight forward – we are populating the $A variable with an array of service objects. Line 2 is the neat trick: we can access the properties of an array, in this case doing a count of the objects in the array (in variable $A) and placing that value into the $Total variable. $Total now holds a string value, and can be used in other cmdlets, calculations and expressions etc. down the pipeline. Line 3 is an example of this where we are just using the Write-Output cmdlet to display the actual contents of $Total.
So there you have it - some truly great ways to easily measure the lines, characters and/or words of a text file, do calculations to find out the average, the sum, minimum and maximum values and so on and even do calculations on the properties of arrays. Have fun trying these examples out for yourself, and let me know what neat measurement options you come up with.