How to time a command in Powershell

Sometimes it is useful to know how long a command or script uses to complete. This is how you do it in Powershell.

Measure-Command -Expression { dir }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 6
Ticks             : 68425
TotalDays         : 7,91956018518518E-08
TotalHours        : 1,90069444444444E-06
TotalMinutes      : 0,000114041666666667
TotalSeconds      : 0,0068425
TotalMilliseconds : 6,8425

If you want to see the output of the command you can do the following:

Example 1: This will tell you how long time the dir command uses to complete:

$sw = [Diagnostics.Stopwatch]::StartNew(); dir; $sw.Stop(); $sw.Elapsed

Example 2: This will tell you how long the script called script.ps1 uses to complete:

$sw = [Diagnostics.Stopwatch]::StartNew(); ./script.ps1; $sw.Stop(); $sw.Elapsed
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *