Test execution speed with Measure-Command

When hacking away at a shell or writing a quick on-off script that will only do a handful of things and only once, knowing how fast each part works isn't that big of a deal.  When you're writing something that will get reused over and over, especially if it's something that's working against a large collection (such as, for example, running a WMI query against every computer in your domain), knowing how quickly different commands run could be quite important, especially if you're looking to improve execution speed.

To test execution speed, I use Measure-Command.

Continuing my above example, lets say I have an automated script that I have run every weekend, and I believe I may be able to make it go faster.  At one point in the script, it performs a WMI call for Win32_ComputerSystem to a list of servers on the domain, and I'm wondering if replacing Get-WmiObject with Get-CimInstance will speed things up.  To test this, first I run a sample query using Get-WmiObject, then run the same query using Get-CimInstance, both times using Measure-Command to capture the execution speed.

(note: there's also the question of compatibility in this example, which I'd have to verify separately; for this demonstration, however, I'm going to ignore that component.  Please note also that as shown, this isn't necessarily a proper test of the speed difference between Get-WmiObject and Get-CimInstance.)

Here's the commands I used with their output:

The first part is just how I got the list of server names; I decided to get them directly from Active Directory here, pulling all of the computers out of the OU I created called Servers.  If you want to test this yourself, replace that line with a list of computer names; note that I don't recommend running this in a production environment without testing first (though the likelihood that you'll cause problems is slim, don't just start hammering queries at production servers)

The second parts are how Measure-Command works.  You just wrap whatever commands you want to test in curly brackets, and instead of putting the results of the command in the output, it will return the output of Measure-Command instead.  (note that it does still put out errors, which is why I'm suppressing them with -ErrorAction Ignore, and any commands that make changes will still make changes; so measuring how fast it will delete your C: drive will delete your C: drive!  It just won't tell you about it.)

The output looks like this:

Note that the top part is the time broken down into each part, while the bottom part (the fields that start with "Total") shows the total time in that unit of measurement.  So, based on that, it took 431 seconds to run the command with Get-WMIObject, and 25 seconds with Get-CIMInstance!

So, assuming that the commands are a fair representative, I now know I can update my script and expect to shave some time off of my script.

For more information on Measure-Command, check out Get-Help Measure-Command.