PowerShell Basics Series– Measuring Objects (Part 1)

 Jun 07, 2016

Have you ever wanted a simple and easy way to count the number of words in text file, check the size of files in a directory (in sorted order) or how much memory your processes are taking up? Well have we got a PowerShell cmdlet for you – it is the Measure-Object cmdlet.

Simply put, the Measure-Object cmdlet will perform the maths required on any object properties piped into it, and give the count, average, sum, maximum, minimum and the property it is calculating e.g.:

By default, only the count is performed, but you can show the other results simply by specifying them in the command line:

The first cmdlet just gets a count of the processes currently running on the local machine, and the second example shows the count, the Average and the Sum of the Virtual Memory (the VM property). Now I know the Average and the Sum values look rather messy (the indicated values are in bytes!) but hold your horses – I will show you a few tricks around that in a couple of minutes.

In the next two examples, I am using a WMI cmdlet to list recursively (i.e. including lower levels) the namespaces in a WMI repository, starting from the root namespace; piping that into a Select-Object cmdlet to eliminate duplicate names and finally give me a count of those unique namespaces. Note two things here: you will need to run these cmdlets under Administrative access, or you will get “Access Denied” errors, and the value I have used in the Select-Object cmdlet (the __Namespace) actually has TWO underscore characters in the front. The double underscore indicate system reserved namespaces.

The only difference between the two lines is I removed the –Unique parameter in the second line – just goes to show how many duplicates there are in the namespaces!

The next example is a easy way to check the size of files in a specified directory. I have used aliases and abbreviations in the example, and particularly note here that the the abbreviation for Average is Ave, and not Avg. Length is the size of the files and properties of files is a positional parameter, so –Property does not need to be specified in the Measure-Object cmdlet.

OK, so how do we make those horrible byte related values display in a more readable form? Well this where we have to do a bit of calculating, and we need to create expressions to do that, but again PowerShell has some tricks we can take advantage of. Lets digress from the Measure-Object cmdlet a moment and look at the following example:

OK, so here we have a list of the processes currently running on the local machine (yes I have truncated the output for the sake of brevity). Then we use the Select-Object cmdlet to show each process’s Name, ID, and two calculated values: Virtual Memory and Physical Memory. The @ symbol indicated an array and the curly braces indicate an expression. n= indicates the expression name or label (you can use name or n or label or l here) with “Virtual Memory” and “Physical Memory”) the name of the expression. e= indicated the actual expression, again in their own curly braces. $PSItem (or you could still use the older $_). VM and PM are properties of each process object i.e. the Virtual Memory and Physical Memory properties. All well and good but the calculated values are again in bytes by default. So here is the same line as above, but modified to include a calculation to change the bytes to Megabytes.

I have added (MB) to the expression name to indicate the values are now in MB’s and included the /1MB to do the actual calculation (by the way PowerShell accepts KB, MB, GB, TB and even PB here). Getting better, but we can take it one step further – the following adds some formatting to the expression to limit the display to 2 decimal places.

OK so I have added ‘{0:N2}’ –f to each expression to do the formatting and enclosed the $PSItem.VM/1MB in parentheses to make sure that the calculated expression itself is evaluated before the formatting. The –f is PowerShell’s formatting operator, and the ‘{0:N2}’ tells PowerShell to display the first data item as a number to two decimal places. You can find out more about the –f formatting operator by running Help About_Operators.

Finally, I have put all of the above examples on one line, but the following spreads this across multiple lines to make it possibly clearer to understand, but the operation is exactly the same (the example shown is displayed in PowerShell ISE).

P.S Stand by for my next blog PowerShell Basics – Measuring Objects (Part 2). I have run out of time and space here but I have another some more great tips on measuring the contents of text files and placing the measurement results into variables, for later use.

How do your Excel skills stack up?   

Test Now  

About the Author:

Gordon Cowser  

With over 22 years real world and training experience, Gordon is our most senior IT Infrastructure trainer. His expertise includes but is not limited to; Microsoft Server and Client OS, Messaging, Collaboration, Active Directory and Network Infrastructure. Gordon also specialises in SharePoint technologies training in both technical and end user aspects. With his extensive skill-set he brings a thorough mentoring capability to the classroom where he can advise on technical issues and challenges often beyond the scope of the course curriculum. A very approachable and experienced training professional, he has the ability to establish credibility fast with students at all levels.

Read full bio
top
Back to top