Time command equivalent in PowerShell Time command equivalent in PowerShell powershell powershell

Time command equivalent in PowerShell


The Measure-Command cmdlet is your friend.

PS> Measure-Command -Expression {dir}

You could also get execution time from the command history (last executed command in this example):

$h = Get-History -Count 1$h.EndExecutionTime - $h.StartExecutionTime


I've been doing this:

Time {npm --version ; node --version}

With this function, which you can put in your $profile file:

function Time([scriptblock]$scriptblock, $name){    <#        .SYNOPSIS            Run the given scriptblock, and say how long it took at the end.        .DESCRIPTION        .PARAMETER scriptBlock            A single computer name or an array of computer names. You mayalso provide IP addresses.        .PARAMETER name            Use this for long scriptBlocks to avoid quoting the entire script block in the final output line        .EXAMPLE            time { ls -recurse}        .EXAMPLE            time { ls -recurse} "All the things"    #>    if (!$stopWatch)    {        $script:stopWatch = new-object System.Diagnostics.StopWatch    }    $stopWatch.Reset()    $stopWatch.Start()    . $scriptblock    $stopWatch.Stop()    if ($name -eq $null) {        $name = "$scriptblock"    }    "Execution time: $($stopWatch.ElapsedMilliseconds) ms for $name"}

Measure-Command works, but it swallows the stdout of the command being run. (Also see Timing a command's execution in PowerShell)


If you need to measure the time taken by something, you can follow this blog entry.

Basically, it suggest to use the .NET StopWatch class:

$sw = [System.Diagnostics.StopWatch]::startNew()# The code you measure$sw.Stop()Write-Host $sw.Elapsed