To kick off the new year I thought I'd take a shortcut and reclaim some wasted space in my PowerShell prompt. I know I run into this issue during classes and conferences. Perhaps you encounter it as well. You are in in the PowerShell console and have ended up in a deep directory structure so that you prompt and console now look like this:
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
As you can see, I don't have a lot of space left at my prompt. Usually, if I am staying in one location say for demos, I'll create a PSDrive with a shorter name. I use the New-PSDriveHere command from the PSScriptTools module. But another option is to truncate the prompt. So instead of the long path you see here it might be C:\Users…\HelpDesk. I modified the default PowerShell prompt function to do just that.
The function is designed to work cross-platform and is pretty simple in design. If the length of the current location is greater than some value, my default is 33, then split the path on the directory separator character. This should be either \ or /. Then construct a shortened path using the first 2 elements of the split array and the last element. In between I'm inserting an ellipse (…) although you could use any thing you'd like.
Watch what happens now when I load the new prompt function:
Now my prompt location dynamically updates.
And it also works cross-platform.
Dot source the prompt function in your PowerShell profile script and you are all set. Let me know what you think or if you encounter any problems. Enjoy and have a great weekend.
1 thought on “Friday Fun: Taking a Shortcut Path in Your PowerShell Prompt”
Comments are closed.