Measuring Website Response (and Execution Times)

by Mar 13, 2015

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"

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.

Twitter This Tip! ReTweet this Tip!