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