- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use stopwatch in PowerShell?
To use the stopwatch in PowerShell, we need to use [System.Diagnostics.Stopwatch] class.
We will create a new object for this class,
$stopwatch = [System.Diagnostics.Stopwatch]::new()
Below are the members of the above stopwatch mentioned class.
PS C:\> $Stopwatch | gm TypeName: System.Diagnostics.Stopwatch Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() Reset Method void Reset() Restart Method void Restart() Start Method void Start() Stop Method void Stop() ToString Method string ToString() Elapsed Property timespan Elapsed {get;} ElapsedMilliseconds Property long ElapsedMilliseconds {get;} ElapsedTicks Property long ElapsedTicks {get;} IsRunning Property bool IsRunning {get;}
You can see above that you can Start, Stop, Restart, and Reset the stopwatch and before starting all values should be 0.
PS C:\> $Stopwatch.Elapsed Days : 0 Hours : 0 Minutes : 0 Seconds : 0 Milliseconds : 0 Ticks : 0 TotalDays : 0 TotalHours : 0 TotalMinutes : 0 TotalSeconds : 0 TotalMilliseconds : 0
Now, we will start the timer using the Start() method.
$Stopwatch.Start()
You can check the elapsed time using the Elapsed property.
PS C:\> $Stopwatch.Elapsed Days : 0 Hours : 0 Minutes : 1 Seconds : 42 Milliseconds : 284 Ticks : 1022842838 TotalDays : 0.00118384587731481 TotalHours : 0.0284123010555556 TotalMinutes : 1.70473806333333 TotalSeconds : 102.2842838 TotalMilliseconds : 102284.2838
To retrieve any of the above property, use the property name. For example,
Example
PS C:\> $Stopwatch.Elapsed.Minutes 4
To stop the stopwatch use Stop(), to Reset the Stopwatch use Reset() methods, and to restart the stopwatch use Restart() method.
PS C:\> $Stopwatch.Reset() PS C:\> $Stopwatch.Elapsed Days : 0 Hours : 0 Minutes : 0 Seconds : 0 Milliseconds : 0 Ticks : 0 TotalDays : 0 TotalHours : 0 TotalMinutes : 0 TotalSeconds : 0 TotalMilliseconds : 0 PS C:\> $Stopwatch.Restart() PS C:\> $Stopwatch.Elapsed Days : 0 Hours : 0 Minutes : 0 Seconds : 1 Milliseconds : 757 Ticks : 17570261 TotalDays : 2.03359502314815E-05 TotalHours : 0.000488062805555556 TotalMinutes : 0.0292837683333333 TotalSeconds : 1.7570261 TotalMilliseconds : 1757.0261 PS C:\> $Stopwatch.Stop() PS C:\> $Stopwatch.Elapsed Days : 0 Hours : 0 Minutes : 6 Seconds : 36 Milliseconds : 121 Ticks : 3961214918 TotalDays : 0.00458473948842593 TotalHours : 0.110033747722222 TotalMinutes : 6.60202486333333 TotalSeconds : 396.1214918 TotalMilliseconds : 396121.4918
Advertisements