Export (0) Print
Expand All
Expand Minimize

Using the Measure-Command Cmdlet

Timing How Long it Takes a Script or Command to Run

For better or worse people are often concerned with the running time of their scripts and their commands. If you’ve ever wondered how long it takes to perform a specific task then the Measure-Command cmdlet is exactly what you’ve been looking for: it enables you to measure the running time of a command or script down to the millisecond. (Actually, it even enables you to measure the running time beyond a millisecond. But at that point ….)

To use Measure-Command, simply call the cmdlet, using the command or script to be run as the cmdlet parameter (make sure you enclose the value within curly braces). For example, this command runs the Get-Service cmdlet and then exports the data to an XML file named C:\Scripts\Test.xml; at the same time, it also keeps track of the time it takes for the command to complete:



Measure-Command {Get-Service | Export-Clixml c:\scripts\test.xml}

When you run this command, service information will be saved to the file Test.xml; in addition, Measure-Command will report back information similar to this:



Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 980
Ticks             : 9807034
TotalDays         : 1.13507337962963E-05
TotalHours        : 0.000272417611111111
TotalMinutes      : 0.0163450566666667
TotalSeconds      : 0.9807034
TotalMilliseconds : 980.7034

To perform this same feat with a script simply pass the name of that script as the Measure-Command parameter. This command runs the script C:\Scripts\Test.ps1, and - as a bonus - reports back the running time as well:



Measure-Command {c:\scripts\test.ps1}

Here’s what we get back:



Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 971
Ticks             : 9717422
TotalDays         : 1.12470162037037E-05
TotalHours        : 0.000269928388888889
TotalMinutes      : 0.0161957033333333
TotalSeconds      : 0.9717422
TotalMilliseconds : 971.7422

As you can see, at least in this case there’s no practical difference between running the command from the Windows PowerShell console and running that very same command from a Windows PowerShell script.

Related Links

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft