Show Elapsed Time on Console Title bar

Hello Everyone,

I am trying display elapsed time on powershell console Title bar. I have two functions first one displays the clock and works fine. The second one displays elapsed time but the issue i am running it does not let me run any commands on the PS console while displaying elapsed time. However the first function that displays the clock and i can run any other commands on the PS console.

Any help greatly appreciated.

First function:

function Start-Clock
{
  # create a timer that fires every 300ms
  $script:timer = New-Object System.Timers.Timer
  $timer.Enabled = $true
  $timer.Interval = 300
  $timer.AutoReset = $true

  # respond to the timer "Elapsed" event
   $null = Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Clock -Action {
    # execute this whenever the timer fires
    $titleText = $host.Ui.RawUI.WindowTitle
    
    # is there a date information displayed already?
    $hasTime =  $titleText -match '^\[\d{2}:\d{2}:\d{2}\] - '
    if ($hasTime) 
    {
      # remove old date
      $titleText = $titleText.SubString(13)
    }
    # set new date
    $time = '[' + (Get-Date -Format 'HH:mm:ss' ) + '] - '
    $host.UI.RawUI.WindowTitle = $time + $titleText
  }
}
Start-Clock

Second Function:

function Start-ElapsedTimer
{
  # create a timer that fires every 3 Seconds
  $script:timer = New-Object System.Timers.Timer
  $timer.Enabled = $true
  $timer.Interval = 3000
  $timer.AutoReset = $true

  # respond to the timer "Elapsed" event
   $null = Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Clock -Action {
    # execute this whenever the timer fires
    $titleText = $host.Ui.RawUI.WindowTitle
    
    # is there a ElapsedTimer information displayed already?
    $hasElapsedTimer =  $titleText -match '^\[\d{2}:\d{2}:\d{2}\] - '
    if ($hasElapsedTimerTime) 
    {
      # remove old ElapsedTimer
      $titleText = $titleText.SubString(13)
    }
    # set new ElapsedTimer
    $elapsedTime = [system.diagnostics.stopwatch]::StartNew()
     while($true)
	{
    $displayetimer = 'Elapsed Time [' + ($elapsedtime.Elapsed.ToString('hh\:mm\:ss')) + '] - '
	sleep 1
    $host.UI.RawUI.WindowTitle = $displayetimer + $titleText
  }
}
}

Start-ElapsedTimer

The second function has an infinite while loop, hence it will not come out and you will not be able to enter until the while loop finishes.

is there any other way to accomplish this? Showing elapsed timer either on the title bar or on the stop of PS console? I am just trying display how long is the script running so operators can take action if it is running for a long time. Thanks.

Have you looked at Write-Progress: PowerShell's Write-Progress Explored -- Microsoft Certified Professional Magazine Online

If you want to measure how much time a script/cmdlet took to complete execution, you can use Measure-Command

Measure-Command {sleep 10}

Get-Help Measure-Command -Full

As @Rob mentioned, Write-Progress will be a great value add for you.

You can put virtually whatever string you want in the title bar…

Example:

https://learn-powershell.net/2012/01/28/turn-your-powershell-console-title-bar-into-a-rssweather-ticker
...but you also have to clean that up when you are done with whatever you did.

Adding date, time , timers is a similar approach…

http://community.idera.com/powershell/powertips/b/tips/posts/add-a-clock-to-powershell

Adding Clock to PowerShell Console | IderaBlog


As far as measuring how long a script took, that is a common test done and documented all over the web.

Example:

https://www.pluralsight.com/blog/tutorials/measure-powershell-scripts-speed