Simple stopwatch for your scripts

A really simple way to keep track of how long various parts of your script take to complete, or your entire script... or both... is to use the .NET stopwatch object.

Here's how

#Create the object, and assign to a variable
 $stopwatch = new-object -TypeName system.diagnostics.stopwatch

#Start the stopwatch

#Start whatever you want to time
get-wmiobject Win32_ComputerSystem

#Stop the stopwatch

#Display the time taken
"Time Elapsed: " + $stopwatch.elapsed.TotalSeconds

It's that simple.

You don't have to display the time taken to the screen, you could use it to make decisions during your script, such as "if it takes longer than 1 minute, give up, or throw an error", for example.

This super-fast post took 1 minute and 47 seconds to complete :)