Author Posts

August 31, 2018 at 7:21 pm

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

August 31, 2018 at 7:37 pm

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.

August 31, 2018 at 7:41 pm

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.

August 31, 2018 at 8:09 pm

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

August 31, 2018 at 8:11 pm

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