As our ability as a PowerShell developer grows, and the complexity of our scripts improve, we begin incorporating new components to enhance the consumer expertise. That may embody altering fonts, the background colour, or the console window title. This activity was already mentioned in a weblog publish from 2004, Can I Change the Command Window Title When Operating a Script?. Nonetheless, the publish makes use of VB script, and adjustments the title in case you are keen to open a brand new console. Right this moment we discover ways to do it with PowerShell, utilizing the identical window.
Strategies
We’ll discover two methods of adjusting the console window title.
- The
$Host
automated variable. - Console digital terminal sequences.
The $Host automated variable
This variable accommodates an object that represents the present host software for PowerShell. This object accommodates a property referred to as $Host.UI.RawUI
that permits us to vary numerous points of the present PowerShell host, together with the window title. Right here is how we do it.
$Host.UI.RawUI.WindowTitle="MyCoolWindowTitle!"
And with only a property worth change our window title modified.
For as easy and straight ahead the earlier methodology is, there’s something to bear in mind. The $Host
automated variable is host dependent.
Digital terminal sequences
Console digital terminal sequences are management character sequences that may management numerous points of the console when written to the output stream. The terminal sequences are intercepted by the console host when written into the output stream. To see all sequences, and extra in-depth examples go to the Microsoft documentation web page. Digital terminal sequences are most well-liked as a result of they comply with a well-defined customary, and are absolutely documented. The window title is proscribed to 255 characters.
To vary the window title the sequence is ESC]0;<string><ST>
or ESC]2;<string><ST>
, the place
ESC
is character 0x1B.<ST>
is the string terminator, which on this case is the “Bell” character 0x7.
The bell character may also be used with the escape sequence a
. Right here is how we alter a console window title with digital terminal sequences.
$title="Title with terminal sequences!"
Write-Host "$([char]0x1B)]0;$title$([char]0x7)"
# Utilizing the escape sequence.
Write-Host "$([char]0x1B)]0;$title`a"
Conclusion
PowerShell is a flexible instrument that always offers a number of methods of attaining the identical purpose. I hope you had as a lot enjoyable studying as I had writing. See you within the subsequent one.
Completely happy scripting!
Helpful hyperlinks:
Check our PowerShell module: