Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: Taking a Shortcut Path in Your PowerShell Prompt

Posted on January 3, 2020January 3, 2020

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:

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

The long PowerShell prompt problem

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:

A shortened PowerShell location

Now my prompt location dynamically updates.

Dynamic location length

And it also works cross-platform.

Cross-platform shortening

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

1 thought on “Friday Fun: Taking a Shortcut Path in Your PowerShell Prompt”

  1. Pingback: Truncating Paths in Powershell Prompts – Curated SQL

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d