Measure execution time of a command on the Windows command line

Follow @desiprogrammer_

Table Of Contents

Requirement Solution


I wanted to see if there is an alternative to time command on windows too, using which i can find execution time of a command. Did some search , found out a solution and in this Blog , I am mentioning it for future use or I hope it helps someone too.


PowerShell has a cmdlet for this called Measure-Command. And since powershell is available on almost all newer windows (including and after win 7), we are safe to go with this.

Measure-Command { echo hi }

Measure-Command captures the command's output.So h1 won't be displayed on your terminal. You can redirect the output back to your console using Out-Default.

PS> Measure-Command { echo hi | Out-Default }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 1318
TotalDays         : 1.52546296296296E-09
TotalHours        : 3.66111111111111E-08
TotalMinutes      : 2.19666666666667E-06
TotalSeconds      : 0.0001318
TotalMilliseconds : 0.1318

This command returns a TimeSpan object, so the measured time is printed in such Detail . You can format the object into a timestamp string using ToString().

PS> (Measure-Command { echo hi | Out-Default }).ToString()

NOTE : If the command inside Measure-Command changes your console text color, use [Console]::ResetColor() to reset it back to normal.

So for instance, If you want to see how much time npx takes to create a react js project , you can use.

Measure-Command { npx create-react-app testapp | Out-Default }
# (Measure-Command { npx create-react-app testapp | Out-Default }).ToString()

Or , if you want to know , How much time does your system takes to build a flutter apk, You can use.

Measure-Command { flutter build apk | Out-Default }