Solved

Customize powershell window title

Posted on 2016-07-15
4
75 Views
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()
$host.ui.rawui.set_WindowTitle($NewTitle)

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
"c:/Users/Derek/Documents/WindowsPowerShell/Modules/Open-Xml-PowerTools-vNext/OpenXmlPowerToolsExamples/"

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 :
"c:/Users/Gene/Documents.../Modules/Open-Xml-PowerTools-vNext/OpenXmlPowerToolsExamples/"

How can this be done in PowerShell (auto change title & new title text)?
Thanks!
0
Comment
Question by:ugeb
[X]
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
4 Comments
 
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()
$host.ui.rawui.set_WindowTitle($NewTitle)
}

Open in new window

Done!
0
 
LVL 11

Author Comment

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

Accepted Solution

by:
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}
$host.ui.rawui.set_WindowTitle($dir)
}

Open in new window

0
 
LVL 11

Author Closing Comment

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

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 (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) 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