I have having my PowerShell prompt be the really long directory name it sometimes is, so I put it in my title. In my profile.ps1 file I have the following lines to set my window title to my current path:
$NewTitle="Dir: " + (get-location).toString()
This code has two problems. The first is that it only seems to work on startup of a shell and isn't refreshed with each command or change of directory. The second problem is that even this can be too long to fit in my title. How can I add a function to my ps1 file to change my window title as follows:
1) If the whole directory path fits in the window title, use it
2) If the path doesn't fit, make the window title the following:
a) First 3 directories, e.g. "C:\Users\Derek" + ...
b) The remainder of the title to be the path that fits in the title, starting from the end.
If, for example, my path is
This is about 102 characters and let's say the title can only be 75 (I don't know the real #).
With these, I would want my window title to be :
How can this be done in PowerShell (auto change title & new title text)?