I’m at a newer stage of life, and no, this has nothing to do with the current, worldwide pandemic or staying at home on a daily basis now. It’s hardly that serious. It has everything to do with my computer and the plethora of PowerShell tabs I have open. More so these days, it’s actually the elevated number of Windows Terminal instances running on my single, physical laptop. Sure, I could keep everything in a single instance of Windows Terminal, but I’m a fan of using multiple desktops. For me for example, it’s “here’s my general desktop, here’s my Bitbucket repository desktop, here’s my VS Code and Git desktop, and here’s my Docker desktop.” That’s four instances of Windows Terminal, although my VS Code and Git desktop would only have a Git Bash tab in Windows Terminal. Anyway, I needed a quicker way to know which desktop I’m currently on, and since I’m often at a PowerShell prompt, I thought I should help myself out.
This took me back to my earlier days learning PowerShell. See, those things really matter. I took advantage of my knowledge of the host program’s title bar. I was already doing that, but I needed to add more to it. Anyway, here’s what you’d see by default after I opened Windows PowerShell (5.1) and PowerShell (7.0.0), and then switched back to PowerShell.
As you can see, I use the tabs to ensure I know which version of PowerShell I’m using, although it’s mostly clear anyway due to the icons, host background color, etc. So yes, I have my version as clear as can be, but that’s not really the point today. I want to know which desktop I’m on with some help from PowerShell.
In order to know which desktop I’m currently using, I need to alter the WindowTitle. To be exact, I need to alter the $Host.UI.RawUI.WindowTitle. The value assigned to this property of this variable is displayed in the console host’s title, or in Windows Terminal, right on the tab. In this image, the WindowTitle simply says “PS 7.” I’ve been good with that, but now I want more.
The below function is the one I quickly authored for this purpose. It will allow me to alter the title, so that what I want is added to the already existing value, such as “PS 7” and “WPS 5.1.” Additionally, I added parameter sets and a second parameter, in order that I can reset the title back to its original value, and not just add something new.
Function Update-WindowTitle { [CmdletBinding()] Param ( [Parameter(Mandatory, ParameterSetName = 'Add')] [string]$AdditionalTitle, [Parameter(ParameterSetName = 'Reset')] [switch]$Reset ) # End Param. If (-Not(Get-Variable -Name OriginalTitle -Scope Global -ErrorAction SilentlyContinue)) { New-Variable -Name OriginalTitle -Value $Host.UI.RawUI.WindowTitle -Option Constant -Scope Global } # End If. If ($AdditionalTitle) { $Host.UI.RawUI.WindowTitle = "$OriginalTitle | $AdditionalTitle" } ElseIf ($Reset) { $Host.UI.RawUI.WindowTitle = $OriginalTitle } # End If-Else. } # End Function: Update-WindowTitle.
Here’s a series of images that walk through some edits of the WindowTitle. I updated the title to show Docker, then Pandoc, and finally, reset its value. With multiple instances of Windows Terminal, on multiple virtual desktops, I can now quickly remind myself of the desktop I’m currently using. Although the tab title values are slightly different, I’ve included a gif below as well, in order to show movement between the desktops.
There’s more I could add to this function to ensure it works under odd conditions, but for now and for me, it’ll work just fine. Now to add it to my $PROFILE, so it’s always available.