Nov 16

PowerShell 101: Easy Script Timing

There are times when you want to know how long it takes for a script to run. One of the Measure-* cmdlets can be useful, but there is a simpler way to time how long it takes to run a script (or piece of code).
The StopWatch .NET method is an ideal method for script timing.

More info on using the StopWatch .Net method:
http://technet.microsoft.com/en-us/magazine/2009.03.heyscriptingguy.aspx

# START OF SCRIPT
$CurrentUserName = $env:UserName
$ScriptProcessStartTime = Get-Date
write-output “” `r
write-output “Script initialized by $CurrentUserName and started processing at $ScriptProcessStartTime `r ”
$ScriptTimer = [System.Diagnostics.Stopwatch]::StartNew()
write-output “” `r

## SCRIPT BODY

## END OF SCRIPT
$ScriptProcessEndTime = Get-Date
write-output “Script started processing at $ScriptProcessStartTime and completed at $ScriptProcessEndTime.” `r
write-host “Total Processing Time: $($ScriptTimer.Elapsed.ToString())”

 

(Visited 179 times, 1 visits today)