PowerShell Measure-Command Cmdlet

PowerShell Measure-Command

When I want to improve the speed of my scripts I employ Measure-Command to count time.  There is a sister cmdlet called Measure-Object which deals with maths, such as counting files.

Topics for Measure-Command

This is PowerShell's equivalent of 'Time' in Bash; Measure-Command is a more convenient than Microsoft’s resource kit’s TimeIT.

 ♣

PowerShell Measure-Command

Searching for the correct WMI class with Get-WmiObject can take so long that I begin to question if I have used the wrong syntax.  Here are two methods for researching the names of WMI classes, let us see which script is the quicker.

Method 1 Where-Object
I want to find all the WMI classes containing ‘OperatingSystem’.

# Method 1: Using the ‘Where’ clause
Measure-Command {
Get-WmiObject -List |
Where-Object {$_.name -Match "OperatingSystem"}
}

Result: 208 milliseconds (slow).

Note 1: Observe the structure of this timing technique:
Measure-Command {Script to time}

Method 2 Use Wildcards
When I was a beginner it took me ages to master Where-Object.  Now as a more experienced script writer I have discovered the benefits of employing alternatives to the Where clause.

# Method 2 Finding WMI classes using wildcards
Clear-Host
Measure-Command {
Get-WmiObject -Class "Win32_*OperatingSystem*" -List
}

Result: 48 milliseconds (faster).

Note 2: You will get different values, but I bet there will be a clear pattern, method 1, which incorporates Where-Object, takes 5 times longer than method 2 which uses wildcards in the Win32 class name.

Note 3: Running the script for a second time will result in a faster time due to caching.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network 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.

What I like best is the way NPM suggests solutions to network problems.  Its also has 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 try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Background on the WMI Command

Here below is the information I wanted to find with:
Get-WmiObject -Class "Win32_*OperatingSystem*" -List

Win32_OperatingSystem
Win32_OperatingSystemAutochkSetting
Win32_SystemOperatingSystem
Win32_OperatingSystemQFE

Research More Options for Measure-Command

You may have already noted the main properties in the units of time displayed in the output; however, here is the command to list the properties and methods.

# Check properties and methods
Clear-Host
Help Measure-Command {Get-Help} | Get-Member

Note 4: You need any valid PowerShell command inside the {braces} for GM (Get-Member) to display the properties.

To display the results only in milliseconds try encasing the whole command in (parenthesis) then append .milliseconds

# Dot .milliseconds
Clear-Host
(Measure-Command {Get-Help}).Milliseconds

Note 5: Try other units of time such as .Ticks

Get More Examples and Check Measure-Command’s Parameters

This is a rare case where Get-Help revealed no interesting parameters, but if you want to see more examples try this trusty method:

# Research Measure-Object’s properties
Clear-Host
Get-Help Measure-Command -full

Research Similar ‘Measure’ Cmdlets

# Discover more of PowerShell’s ‘Measure’ Cmdlets
Clear-Host
Get-Command -verb Measure

See the sister cmdlet Measure-Object ยป

Summary of PowerShell Measure-Command

When you write PowerShell scripts to solve computer problems there are usually alternative techniques, the benefit of Measure-Command is that you can see which option executes fastest.  However, if it’s maths such as average file size, then call for the sister command Measure-Object.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell Examples of Real Life Tasks

PowerShell Real-life Examples   • Test-Connection   • Invoke-Expression   • Invoke-Command

Windows PowerShell   • Com   • PowerShell -Filter Where-Object   • PowerShell 3 Rename-Computer

PowerShell Registry   • Compare-Object Registry   • Measure-Object   • Measure-Command

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.