(updated ) by

PowerShell Stopwatch

One component of metrics you can take is measuring how long your scripts take to run. I have a few scripts for powershell and wanted to have a line tell me how long it took.

Start

$sw = [Diagnostics.Stopwatch]::StartNew()

Toss this at the start. Setting the $sw variable to refer to at the end of the script.

Stop

$sw.Stop()

Output

Then output the elapsed time.

$elapsed = $sw.Elapsed.TotalSeconds

Write-Host "Ran $elapsed seconds"

Full

$sw = [Diagnostics.Stopwatch]::StartNew()

...

$sw.Stop()

$elapsed = $sw.Elapsed.TotalSeconds

Write-Host "Ran $elapsed seconds"