MS Stopwatch Class


The Microsoft System.Diagnostics.Stopwatch provides a set of methods and properties that can be used to accurately measure elapsed time.


The following PowerShell code demonstrates this in use.

$sw = New-Object System.Diagnostics.Stopwatch;
# some work or go to sleep...
Write-Host "Sleeping for $sleepseconds seconds";
Start-Sleep -Seconds $sleepseconds;

Sample output from this code looks something like this.

Sleeping for 14 seconds

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 14
Milliseconds      : 17
Ticks             : 140173533
TotalDays         : 0.000162237885416667
TotalHours        : 0.00389370925
TotalMinutes      : 0.233622555
TotalSeconds      : 14.0173533
TotalMilliseconds : 14017.3533

The highlighted line showing the number of seconds that we’ve slept for. In our example, 14 seconds.

See also

MSDN Library documentation Stopwatch Class

MSDN TechNet article Using the Start-Sleep Cmdlet

Windows PowerShell Cmdlet Help Topics Start-Sleep

This entry was posted in powershell and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s