Change Window title in PowerShell while cd😀

in #coding4 years ago

I started using an open-source SW, ActivityWatch, where we can monitor our activities based on the window title and afk based presence detector. I've been tinkering with it for a while. For that purpose, I've been trying to name the window title of my powershell terminal, running on Alacritty. Alacritty could only give control to powershell to change the window title. So i searched online and found a simple snippet to change the window title.

$host.ui.RawUI.WindowTitle = $pwd

I was trying to run it as an external command with starship prompt, but it was taking a long time. Then I realized that I was using a powershell function to change the directory and so I added the snippet in that function only to change window title when I enter any new directory.

function Set-LocationWithGCI{
  [string]$path = $args
  Set-Location $path
  ls --color
  $host.ui.RawUI.WindowTitle =
    $driveHashTable[$pwd.drive.name] + " " + $pwd.Drive.CurrentLocation
} Set-Alias cd Set-LocationWithGCI

You could also combine it with other projects like zoxide.

function Set-LocationWithGCI{
  __zoxide_z $args
  ls --color
  $host.ui.RawUI.WindowTitle =
    $driveHashTable[$pwd.drive.name] + " " + $pwd.Drive.CurrentLocation
} Set-Alias cd Set-LocationWithGCI