Change the Console Window Title – Part 1

This post will introduce the $Host automatic variable, its properties, and how we can use it to display the date in the Window PowerShell console’s window title.

The $Host variable is an automatic variable and an automatic variable in Windows PowerShell stores “state information” about the PowerShell session. These types of variables are created and maintained by PowerShell. The example below shows the properties of the $Host automatic variable. We can return this same information by also using the Get-Host cmdlet, as seen in the second example.

PS C:\> $Host

Name             : ConsoleHost
Version          : 3.0
InstanceId       : 30d047c2-0565-4947-8f34-7ff2b42d3590
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

PS C:\> Get-Host

Name             : ConsoleHost
Version          : 3.0
InstanceId       : 30d047c2-0565-4947-8f34-7ff2b42d3590
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

Note: Please not use the $Host variable, or the Get-Host cmdlet, to determine the installed version of Windows PowerShell. This variable does not return the version of Windows PowerShell; it returns the version of the host. To determine the version of Windows PowerShell use the $PSVersionTable variable and the PSVersion property.

The UI property in the previous examples has several words separated by dots. This means that the UI property is actually a collection. In most cases, a collection stores a collection of properties. The example below shows how to enumerate the UI property so we can see the properties contained in this collection.

PS PS C:\> $Host.UI

RawUI
-----
System.Management.Automation.Internal.Host.InternalHostRawUserInterface

Wait, what? The UI collection contained RawUI – another collection. After further enumeration, this collection returns several properties. At the bottom of property list is, a property called, “WindowTitle.” This property stores the value used in the window title of the PowerShell console.

PS C:\> $Host.UI.RawUI

ForegroundColor       : DarkYellow
BackgroundColor       : DarkMagenta
CursorPosition        : 0,1
WindowPosition        : 0,0
CursorSize            : 25
BufferSize            : 120,3000
WindowSize            : 120,50
MaxWindowSize         : 120,85
MaxPhysicalWindowSize : 240,85
KeyAvailable          : False
WindowTitle           : Administrator: Windows PowerShell

In the image below, the window title accurately matches what is stored in this property.

Windows PowerShell Console Window Title 05 - Part 1

The title can be changed by assigning a different value to the property $Host.UI.RawUI.WindowTitle. In this example, the window title changes instantly when we assign a new value to the property.

PS C:\> $Host.UI.RawUI.WindowTitle = 'The new and improved title!'
PS C:\>

Windows PowerShell Console Window Title 06 - Part 1

Our goal is to keep the default value, Administrator: Windows PowerShell, append a space, and then the date, inside square brackets. We want it to look like this, “Administrator: Windows PowerShell: [06/19/2014].” To reset the value to its default, close the current PowerShell console and then open a new PowerShell console. Think about that for a moment… This value is going to reset itself to the default value every time a new PowerShell session is created, such as when a new console window is opened. We will discuss this more in a moment.

In the next two examples, the value of $Host.UI.RawUI.WindowTitle is changed by concatenating the date, the space, and those brackets, to the already existing value of this property. Notice that we do this using the += assignment operator. These two examples do the exact same thing.

PS> $Date = Get-Date
PS> $Host.UI.RawUI.WindowTitle += " [$Date]"
PS>

Windows PowerShell Console Window Title 07 - Part 1

PS C:\> $Host.UI.RawUI.WindowTitle += " [$(Get-Date)]"
PS C:\>

Windows PowerShell Console Window Title 08 - Part 1

The time indicates the time in which the PowerShell session started – pretty useless. Using the ToShortDateString method returns the date without the time. Both of the next two examples do the same thing.

PS C:\> $Date = (Get-Date).ToShortDateString()
PS C:\> $Host.UI.RawUI.WindowTitle += " [$Date]"
PS C:\>
PS C:\> $Host.UI.RawUI.WindowTitle += " [$((Get-Date).ToShortDateString())]"
PS C:\>

Windows PowerShell Console Window Title 09 - Part 1

As I mentioned previously, the value that is stored in $Host.UI.RawUI.WindowTitle returns to its default each time a new Windows PowerShell session is opened. In order for this to stick, it needs to be added to a profile. A profile runs each time you start a new PowerShell session. If you’re not already using a profile, then it’s time to begin. Start by reading this article: http://technet.microsoft.com/en-us/library/ee692764.aspx and then follow that up by reading the help file: Get-Help about_Profiles.

Edit: You can also read the recently added Help Rewrite for about_Profiles here: http://tommymaynard.com/hr-about_profiles/

PS C:\> Get-Help about_Profiles

That is all there is to change the Windows PowerShell console’s window title to the current date. Watch for an upcoming post that will help determine how to update the date if the Windows PowerShell session is open for more than one day.

Leave a Reply

Your email address will not be published. Required fields are marked *