Customize powershell window title

Posted on 2016-07-15
Last Modified: 2016-07-15
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()

Open in new window

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)?
Question by:ugeb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 82

Expert Comment

by:David Johnson, CD, MVP
ID: 41713668
wrap it in a function called 'prompt'
function prompt {
$NewTitle="Dir: " + (get-location).toString()

Open in new window

LVL 11

Author Comment

ID: 41713673
This doesn't address the real question of how to have the shortened text. How do I do that?
LVL 82

Accepted Solution

David Johnson, CD, MVP earned 500 total points
ID: 41713690
function prompt {
$newtitle = 'Dir: '+ (get-location).tostring()
if (($newtitle.Length) -ge 75){
    $split = $newtitle.split('/')
    $dir = $split[0]+ '\'+ $split[1]+'\'+$split[2] + '\'+ $split[3] + '\...\'+$split[$split.count-3] + '\'+$split[$split.Count-2]
    if ($dir.Length -ge 75) {
        $dir = $split[0]+ '\'+ $split[1]+'\'+$split[2] + '\'+ $split[3] + '\...\'+$split[$split.Count-2]
else {$dir= $newtitle}

Open in new window

LVL 11

Author Closing Comment

ID: 41713727
That works great, thank you!  (Worked perfectly once I found the small typo in split using "/" instead of "\").
Thanks again!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question