Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Customize powershell window title

Posted on 2016-07-15
4
Medium Priority
?
87 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 83

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 83

Accepted Solution

by:
David Johnson, CD, MVP earned 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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…

722 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