Measure is one of PowerShell's more unusual verbs, it deals with maths
such as sum, average and maximum. Thanks to the concept of an object this cmdlet can also
calculate properties of text
objects, such as the number of words in a document.
In the case of Windows 7 and Server
2008, you don't need to download any extra files, just 'Add Feature' Windows PowerShell.
However, for older operating systems, installing can be confusing because
there are different versions of PowerShell for XP, Windows Server 2003 and
Vista. For such legacy systems only, you need to
download PowerShell from Microsoft's site.
Once you have installed PowerShell 2.0, I recommend choosing the
ISE (Integrated Scripting Engine) version, it
will save you buying a text editor.
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.
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
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)
SolarWinds'
Orion 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.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is 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 take advantage of SolarWinds' offer.
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) Clear-host 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.
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.
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 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 to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
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.
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.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, SolarWinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.