Here’s a handy method to measure elapsed time in powershell. To do this, we will utilize the following two classes:
System.DateTime
System.TimeSpan
Assuming that the variable “$script:StartTime” was set at the beginning of your script, elapsed time can be determined using either of the following methods:
$elapsedTime = new-timespan $script:StartTime $(get-date)
or
$elapsedTime = $(get-date) - $script:StartTime
Both methods work exactly the same and produce a System.TimeSpan object.
Here’s a sample script to demonstrate the process:
$script:startTime = get-date function GetElapsedTime() { $runtime = $(get-date) - $script:StartTime $retStr = [string]::format("{0} days, {1} hours, {2} minutes, {3}.{4} seconds", ` $runtime.Days, ` $runtime.Hours, ` $runtime.Minutes, ` $runtime.Seconds, ` $runtime.Milliseconds) $retStr } write-host "Script Started at $script:startTime" for ($i=1; $i -lt 10; $i++) { get-process | out-null sleep 1 write-host " Elapsed Time: $(GetElapsedTime)" } write-host "Script Ended at $(get-date)" write-host "Total Elapsed Time: $(GetElapsedTime)" |
Just another example of how extremely cool Powershell is!
UPDATE
And here’s an easier, more concise, and even better alternative for measuring elapsed time using the “System.Diagnostics.Stopwatch” class:
(Kudos & Thanks to Stephen Mills for this contribution)
$ElapsedTime = [System.Diagnostics.Stopwatch]::StartNew() write-host "Script Started at $(get-date)" for ($i=1; $i -lt 10; $i++) { get-process | out-null sleep 1 write-host " Elapsed Time: $($ElapsedTime.Elapsed.ToString())" } write-host "Script Ended at $(get-date)" write-host "Total Elapsed Time: $($ElapsedTime.Elapsed.ToString())" |
Sample Output:
Script Started at 03/30/2010 07:42:27 Elapsed Time: 00:00:01.3665694 Elapsed Time: 00:00:02.3825139 Elapsed Time: 00:00:03.3661775 Elapsed Time: 00:00:04.3726722 Elapsed Time: 00:00:05.2734151 Elapsed Time: 00:00:06.2691990 Elapsed Time: 00:00:07.2806800 Elapsed Time: 00:00:08.2851789 Elapsed Time: 00:00:09.2956065 Script Ended at 03/30/2010 07:42:37 Total Elapsed Time: 00:00:09.3097072
Note that the “Elapsed” property returns values similar to the “TimeSpan” class, so you still have the option of formatting the output if so desired”
PS C:\powershell> $Elapsed = [System.Diagnostics.Stopwatch]::StartNew() PS C:\powershell> $elapsed.elapsed Days : 0 Hours : 0 Minutes : 0 Seconds : 11 Milliseconds : 672 Ticks : 116729232 TotalDays : 0.000135103277777778 TotalHours : 0.00324247866666667 TotalMinutes : 0.19454872 TotalSeconds : 11.6729232 TotalMilliseconds : 11672.9232
The stopwatch object is probably even easier to use.
$Elapsed = [System.Diagnostics.Stopwatch]::StartNew()
do { sleep 1 } until ( $Elapsed.ElapsedMilliseconds -ge 3000 )
Write-Host “Total Elapsed Time: $($Elapsed.Elapsed.ToString())”
You can also use ToString on timespan objects to return a string in the format of days.hours:minutes:seconds.fractionalSeconds. So if the elapsed time was 1 day, 2 hours, 13 minutes, 22 seconds, and 14 milliseconds, the ToString output would look like:
1.02:13:22.0140000
Awesome (and way better) alternative – I like it!
Thank You Stephen – I’ll update the post to show this method as well.