«

»

Mar 19

Powershell Tips of the week #2 – Measure-Command

In today’s post I will talk about the Measure-Command function in Powershell. This function will tell you exactly how much time it took to run a cmdlet or a script block. The result will be displayed like this:

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 1
Milliseconds      : 140
Ticks             : 11409189
TotalDays         : 1.32050798611111E-05
TotalHours        : 0.000316921916666667
TotalMinutes      : 0.019015315
TotalSeconds      : 1.1409189
TotalMilliseconds : 1140.9189

Like you see it is very detailed and precise (Milliseconds). Now let’s look how to use Measure-Command.

Measure-Command is really easy to use. Simply add your cmdlet or script block between brackets after Measure-Command:

Measure-Command {get-childitem c:\windows}

Result:
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 8
Milliseconds      : 618
Ticks             : 86182763
TotalDays         : 9.9748568287037E-05
TotalHours        : 0.00239396563888889
TotalMinutes      : 0.143637938333333
TotalSeconds      : 8.6182763
TotalMilliseconds : 8618.2763

Example with Script Block:

Measure-Command{
    $VarA = Get-ChildItem C:\ -Recurse

    foreach($VarB in $VarA){
        Write-Host $VarB.Name  
    }
}


Result:
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 1
Milliseconds      : 140
Ticks             : 11409189
TotalDays         : 1.32050798611111E-05
TotalHours        : 0.000316921916666667
TotalMinutes      : 0.019015315
TotalSeconds      : 1.1409189
TotalMilliseconds : 1140.9189

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>