Show Elapsed Time on Console Title bar

Welcome Forums General PowerShell Q&A Show Elapsed Time on Console Title bar

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
11 months, 3 weeks ago.

  • Author
    Posts
  • #110674

    Participant
    Topics: 1
    Replies: 1
    Points: 0
    Rank: Member

    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
    
  • #110686

    Senior Moderator
    Topics: 8
    Replies: 1008
    Points: 3,275
    Helping Hand
    Rank: Community Hero

    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.

    • #110689

      Participant
      Topics: 1
      Replies: 1
      Points: 0
      Rank: Member

      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.

  • #110692

    Participant
    Topics: 8
    Replies: 1191
    Points: 646
    Helping Hand
    Rank: Major Contributor
  • #110695

    Senior Moderator
    Topics: 8
    Replies: 1008
    Points: 3,275
    Helping Hand
    Rank: Community Hero

    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

  • #110696

    Senior Moderator
    Topics: 8
    Replies: 1008
    Points: 3,275
    Helping Hand
    Rank: Community Hero

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

  • #110705

    Participant
    Topics: 2
    Replies: 999
    Points: 1,946
    Helping Hand
    Rank: Community Hero

The topic ‘Show Elapsed Time on Console Title bar’ is closed to new replies.