Skip to content

Instantly share code, notes, and snippets.

@Goadstir
Last active April 17, 2024 08:18
Show Gist options
  • Select an option

  • Save Goadstir/ae5cc820522ef9bf210d9e9fedf5ed59 to your computer and use it in GitHub Desktop.

Select an option

Save Goadstir/ae5cc820522ef9bf210d9e9fedf5ed59 to your computer and use it in GitHub Desktop.
PowerShell: Using Timer to track time elapsed
$timeout = New-TimeSpan -Seconds 30
$timer = [Diagnostics.Stopwatch]::new() # Create a timer
$timerAndStart = [Diagnostics.Stopwatch]::StartNew() # Create a timer and start it
$timer.Start() # Start the timer
$timer.Stop() # Stop the timer
$timer.Elapsed # Get time elapsed
$timer.IsRunning # Is timer running?
$timer.Restart() # restarts the time, doesn't stop
$timer.Reset() # stops timer and sets to 0
$timer.Equals($timeout) # check if timer value equals a value such as timeout, returns boolean
# How to utilize the timer
$timeout = New-TimeSpan -Seconds 30
$timer = [Diagnostics.Stopwatch]::StartNew() # Create a timer and start it
while ($timer.Elapsed.TotalSeconds -lt $timeout.TotalSeconds) {
# Show elapsed time in console
Write-Output -Message "INFO: Waiting on timeout... Elapsed Time:" $timer.Elapsed.Seconds
# If using Verbose output the "latest" time elapsed must be stored in a var as it cannot be added or used directly with Verbose messaging
# To modify your PowerShell session to show Verbose set the environment variable: $VerbosePreference to: $VerbosePreference = "Continue"
$timeElapsedSeconds = $timer.Elapsed.TotalSeconds
Write-Verbose -Message "INFO: Waiting on timeout... $timeElapsedSeconds"
Start-Sleep -Milliseconds 500
} # While
Write-Verbose -Message "INFO: Timed out!"
@RackoWacko
Copy link

Thank you for the example script.
Powershell is complaining about line 14
Write-Verbose : A positional parameter cannot be found that accepts argument '+'.
It seems the elapsed time for a each cycle of while is not being added to the total Elapsed time.
Thank you for helping me learn.

Days : 0
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 0
Ticks : 67
TotalDays : 7.75462962962963E-11
TotalHours : 1.86111111111111E-09
TotalMinutes : 1.11666666666667E-07
TotalSeconds : 6.7E-06
TotalMilliseconds : 0.0067

False
False
Write-Verbose : A positional parameter cannot be found that accepts argument '+'.
At New-TimerStopWatch.ps1:14 char:5

  • Write-Verbose -Message "INFO: Waiting on timeout..." + $timer.Ela ...
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:) [Write-Verbose], ParameterBindingException
    • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteVerboseCommand

@Goadstir
Copy link
Author

@RackoWacko, Thanks for trying out this Gist and finding the limitation of the Write-Verbose cmdlet. I've updated the Gist to provide a better example of using the Stopwatch diagnostic feature which I hope answers your questions and provides better guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment