Setting PowerShell Title Text

You probably know that you can change the title text of a PowerShell host window with a line like this:

 
PS> $host.UI.RawUI.WindowTitle = "Hello  World!" 
 

When you add this to your prompt function, the title text can be dynamic:

function prompt
{
    # get current path
    $path = Get-Location

    # get current time
    $date = Get-Date -Format 'dddd, MMMM dd'

    # create title text
    $host.UI.RawUI.WindowTitle = ">>$path<< [$date]"

    # output prompt
    'PS> '
}

The function “prompt” is executed each time PowerShell completes processing of a command. In your title bar, you’ll now always see your current path and the date, and the prompt inside the PowerShell editor is shortened to “PS> “.

Twitter This Tip! ReTweet this Tip!

  • I have added something similar to my profile so the title bar tells me if I'm an Administrator or not. quite useful if you sometimes forget to start as such...

  • I often use the ping command with parameter -t to see when a server is up again after a restart, e.g. ping -t 192.168.1.1. After a while you do not see the command any longer (it was scrolled out of the windows). You only see the answers of this permanent ping. With cmd.exe you can still see the command in the title, which is very usefull for me. How can this be achived in PowerShell?

  • aki -

    Are you saying,  you are using this title text approach with ping -t vs just letting it output to the PowerShell Console, since ping and or Test-Connection/Test-NetConnection or using the .NET ping - those lock the console until it is done?

    That would be kind of an odd use for this sort of thing, but I am sure you have your reasons.

    Yet, if you are piping this outout to the titlebar, then you would have to take the approach to continuously update the title with the results you are posting. Meaning, clear the displayed data, then replace with more / other data.

  • @:

    Thanks for your reply. What I mean is not that I want to see the results of the ping command in the title bar, instead the command itself, e.g. "ping -t 192.168.1.1". The same like you see it in cmd.exe.  

    I do not mind that the PowerShell console is locked in such a situation. I can open a new one if I want to do soemthing else with PowerShell.

    To explain why I wnat it:

    If you have more than one PowerShell console open and in each of them a permanent ping (ping -t) to another system is running, e.g. you have calls from different location that internet is down and you ping each of these locations to see when the location is up again. After some time you do not know which Powershell console is pinging to which location, because you see anywhere as a result of the command a timeout and the command itself went out of the buffer from PowerShell.

    Of course we have a professional monitoring, but a ping is fast, easy and handy in such situations. I use it quite often.

  • aki -

    OK, so quick and dirty --- that is as easy as doing this...

    function Start-ContinuousPing ($TargetHost)

    {

       # Display message in titlebar

       $host.UI.RawUI.WindowTitle = "Continuous ping of $TargetHost"

       # Don't display results

       ping -4 -t $TargetHost > $null

    }

    Start-ContinuousPing  -TargetHost '192.168.1.1'

    or if want to have active update, you can do something like this...

    function Start-ContinuousPingUpdate ($TargetHost)

    {

       $CurrentActiveTitle = $host.UI.RawUI.WindowTitle

       $host.UI.RawUI.WindowTitle = $CurrentActiveTitle

       While($true) {

                       $PingInfo = (ping -4 $TargetHost -n 1) -match 'Reply from'

                       $host.UI.RawUI.WindowTitle = $PingInfo

                       Start-Sleep -Seconds 1

                       $host.UI.RawUI.WindowTitle = "Still processing...$TargetHost"

                       Start-Sleep -Seconds 1

                    }

       $host.UI.RawUI.WindowTitle = $CurrentActiveTitle

    }

    Start-ContinuousPingUpdate -TargetHost '192.168.1.1'

    I know... I know.. continuous loops can be a bad thing, but you can set the delay for the timespan you choose to relieve performance hits.