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:

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:

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.