Solved

Customize powershell window title

Posted on 2016-07-15
4
47 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
  • 2
  • 2
4 Comments
 
LVL 78

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 78

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now