Maybe you'd like to include dynamic information such as the current time into the title bar of your PowerShell console. You could update the console title bar inside your prompt function, but then the title bar would only get updated each time you press ENTER. Also, the prompt function may be overridden from other code.

A better way is to spawn another PowerShell thread and let it update the title bar text, preserving whatever has been written to the title bar and just adding the information you want, for example the current time (or, for that matter, the current battery load or CPU load, RSS feed or whatever you want to have an eye on).

Here's the code. Be aware that this will only work inside a real PowerShell console, not inside the ISE editor. After you run this code, call Add-Clock to add the current time to your console title bar.

function Add-Clock {
 $code = { 
    $pattern = '\d{2}:\d{2}:\d{2}'
    do {
      $clock = Get-Date -format 'HH:mm:ss'

      $oldtitle = [system.console]::Title
      if ($oldtitle -match $pattern) {
        $newtitle = $oldtitle -replace $pattern, $clock
      } else {
        $newtitle = "$clock $oldtitle"
      }
      [System.Console]::Title = $newtitle
      Start-Sleep -Seconds 1
    } while ($true)
  }

 $ps = [PowerShell]::Create()
 $null = $ps.AddScript($code)
 $ps.BeginInvoke()
}

Twitter This Tip! ReTweet this Tip!

Anonymous
  • awesome...

    Is there an explanation why the extra dot is needed? I think I remember something about local functions...

  • You need to dot source the script by adding one more period followed by a space:

    . .\AddClock.PS1

    This will add the function to the runspace.

  • I save the script as c:\scripts\AddClock.PS1

    I run the script in the CLI using PS C:\Scripts> .\AddClock.PS1

    That returns nothing.

    When I try Add-Clock, I get a command not found error:

    PS C:\scripts> add-clock

    The term 'add-clock' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the s

    pelling of the name, or if a path was included, verify that the path is correct and try again.

    At line:1 char:10

    + add-clock <<<<

       + CategoryInfo          : ObjectNotFound: (add-clock:String) [], CommandNotFoundException

       + FullyQualifiedErrorId : CommandNotFoundException

  • I modified this script slightly to include a Remove-Clock function; it's not perfect, but it worked for me :)

    function Add-Clock {

    $script:oldtitle = [System.Console]::Title

    $code = {

    $pattern = '\d{2}:\d{2}:\d{2}'

    do {

    $clock = Get-Date -Format 'HH:mm:ss'

    $oldtitle = [system.console]::Title

    if ($oldtitle -match $pattern) {

    $newtitle = $oldtitle -replace $pattern, $clock

    } else {

    $newtitle = "$clock $oldtitle"

    }

    [System.Console]::Title = $newtitle

    Start-Sleep -Seconds 1

    } while ($true)

    }

    $script:ps = [Management.Automation.PowerShell]::Create()

    $null = $script:ps.AddScript($code)

    $script:ps.BeginInvoke()

    }

    function Remove-Clock {

    $script:ps.Stop()

    $script:ps.Dispose()

    [System.Console]::Title = $script:oldtitle

    }