Solved

Customize powershell window title

Posted on 2016-07-15
4
44 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
That works great, thank you!  (Worked perfectly once I found the small typo in split using "/" instead of "\").
Thanks again!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

772 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

12 Experts available now in Live!

Get 1:1 Help Now