Monday, January 13, 2014

Find the amount of time it takes to complete a script

Lately I've been running nightly backups against my main computer at midnight every night which got me wondering . . . how long does it take to complete the backup?

Below are a few lines of code that can be added to any script to find out how long it takes your script to complete.

## Begin the timer.
$StartTime = (Get-Date)

## Begining of code.
hostname ; Get-Counter -Counter "\Memory\Available MBytes" -SampleInterval 1 -MaxSamples 3
## End of code

## Stop timer.
$EndTime = (Get-Date)

## Calculate amount of seconds your code takes to complete.
"Elapsed Time: $(($EndTime - $StartTime).totalseconds) seconds"