PowerShell 3.0 and later
Sometimes it is important to know just how long a command takes. For example, to monitor web site response times, you could use Invoke-WebRequest. Measure-Command measures execution time.
$url = 'http://www.powershell.com'
# track execution time:
$timeTaken = Measure-Command -Expression {
$site = Invoke-WebRequest -Uri $url
}
$milliseconds = $timeTaken.TotalMilliseconds
$milliseconds = [Math]::Round($milliseconds, 1)
"This took $milliseconds ms to execute"
Getting Milliseconds from a TimeSpan and Rounding the Result
It returns a TimeSpan object which in turn contains a property “TotalMilliseconds”. Use Round() provided by the “Math” type to round the result. In this example, the milliseconds are rounded to the first digit after the decimal.
ReTweet this Tip!