Customizing Directory Path in PowerShell Prompt()


Tired of Long unwanted directory paths in Powershell terminal?  Well I’m! Totally tired of looking long directory paths because I spent most of the my time working on Powershell terminals

THE PROBLEM :

When Dealing with long directory paths, we are literally typing on right hand side of your powershell console, moreover console starts wrapping the text when your command hits the edge of the terminal making it even worse to read,  like in the below image.

1

SOLUTION :

There has to be a smarter way, through which we can make custom paths (Aliases) of the Directory names. To achieve this, I’ve quick tip to fix this Using a Hash Table – To Map Directory name to their respective Custom Names like in following Image

hash

and a small Powershell Function to return Custom directory names on basis of above HashTable, which is much shorter in Length.

logic

In short, the function fetch the $PWD (Present Working Directory) and returns the possible alias (If any) defined by you in the HashTable. Following animation demonstrates few examples.

gif

SCRIPT :

HOW TO MAKE  IT WORK :

  1. Go to your Powershell terminal and type “ISE $Profile” to open the Powershell $Profile in ISE.
    ISE $Profile
  2. Copy-Paste the script from the Script category from this blog post (above) to your $Profile, like belowprof
  3. Make the necessary changes in the $CustomDirectories Hashtable, which would be specific to your requirement.
  4. Save the Profile.
  5. Close and reopen ISE in order to make the changes get reflected

NOTE : 

Obviously you’ve a choice to Either Remove the Directory Path from Prompt function or to move it to the Powershell Host Title using below Property.

$Host.UI.RawUI.WindowTitle =  $PWD.Path

But my solution was for when you want to live with Directory path in the Powershell console 🙂


 

Hoping you’ll find useful, have nice day!

Prateek Singh

Advertisements

5 thoughts on “Customizing Directory Path in PowerShell Prompt()

  1. My cheap take was… I added an ending “CRLF” (back tick r, back tick n) to the prompt.
    I got used to seeing my cmd line “below” the looooong path.
    $0.02
    🙂 Marcelo

    Like

  2. VMware Certified Advanced Professional 6 (Desktop and Mobility Deployment) – The industry-recognized VCAP6-DTM Deploy certification validates that you know how to deploy and optimize VMware Horizon 6 (with View) environments. It demonstrates that you have the understanding and skills essential to leverage best practices to provide a scalable and reliable Business Mobility platform for your business. Some of the subject areas include: Configuring and managing Horizon View components, configuring cloud pod archituecture, configuring Group Policy settings related to Horizon View, Configuring and optimizing desktop images for Horizon View & Mirage, Configuring and managing App Volumes AppStacks, Configuring desktop pools, Configuring and deploying ThinApp packaged applications, Configuring VMWare Identity Manager, etc.Sebastian’s take on the VCAP6 examination: “In my viewpoint VCAP6 exam is way better experience when compared with VCAP5, the newest exam appears to be just like VMware HOL. The screen isn’t hard, questions are prepared on the right side of the display, and could be hidden to the side or even restored when desired. My bits of advice to the questions windowpane: if you want to make it floating, you must know how to restore it back. I ended up shifting it around simply because I forget about how to recover it back. The two arrows that looked like buttons on top were intended to dock the window to right or left. Fonts can be resized, which i think was much better than scrolling up and down the question. The response speed of the entire interface was so noticeably faster in comparison with VCAP5.5, and there was no lagging period experienced when transitioning from window to window. One thing to bear in mind: BACKSPACE key is not working! I do believe it is good as you don’t reload your exam window in error, nevertheless, it could be frustrating at times when you type something erroneously and you have to select and press Del to remove. The Desktop and shortcuts were sorted properly, and needed applications like browser or Mirage console could be launched. There’s a excellent interface for Remote Desktop Manager where you can find all necessary RDP connection to servers or desktops without the need to type username and password. The web browser had all the links in the Favorite Bar. Right at that moment I’m writing this, there is no additional Thirty minute extension for Non-Native English speaker at No-Native English country, which is a bummer. There are 39 question to respond within the 3 hours time, which can be actually quite hard for non-native English speakers just like me. Several questions take time to complete, making it wise to neglect the questions that you can’t answer, and finish those you are able to. After the thirty-nine questions, you could visit the uncompleted questions should you have time. Don’t waste too much time on one single question! The exam blue print is found on my blog site at Szumigalski.com. It is well organized and following it for the exam preparation can help a great deal. Obviously, the best is if you can have lots of practical experience! I’m truly very pleased with the exam experience, although I passed this time by small margin, however i really know what I missed for the exam, study from the mistakes and practice harder to familiarise myself with the environment. This credentials is going to open up your career prospects!”

    Like

  3. Nice post – just discovered your blog and I’m tweaking my environment due to your tips.
    I add the current time to my prompt too (where you have the “I heart PS”) so that my transcripts, screenshots etc have the time I was working on a script. That way it gives me a rough idea of how long a command took (assuming that I started to type immediately after the prompt returned instead of 5 minutes later! Very useful when entering data into my timesheet.
    My first line in the prompt function reads as follows:-
    Write-Host $((Date -uformat %T).ToString()) -NoNewline

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s