PowerShell Tricks - Trick 3: Better Prompt!
This one is a small one really, but I found it to be quite useful. One of the things that irritated me was when I would navigate to a folder that is deeply nested under other folders. The prompt would get so damn long that it’s eating up the majority of my command line. So I wrote this small prompt function that handles long folders nicely:
function prompt
{
$pwd = (get-location).path;
if ($pwd.Length -gt 40)
{
$pwd = “…” + $pwd.substring($pwd.Length - 20, 20);
}
if ($IsAdmin)
{
Write-Host (“PS (Admin) “ + $pwd +“>”) -nonewline -foregroundcolor Green
$host.ui.rawui.WindowTitle = “Admin Shell: “ + $(get-location)
}
else
{
Write-Host (“PS “ + $pwd +“>”) -nonewline -foregroundcolor Green
$host.ui.rawui.WindowTitle = “Windows Powershell: “ + $(get-location)
}
return ” “
}
What this does is it checks if the current path is longer than 40 characters, if it is, it truncates it to the last 20 characters preceded by “…”.
So as you can see, the path is nice and short and will never be larger than this size. The window title displays the full path so that you can tell with a glance exactly where you are.
Have fun!
Thanks for reading! I'd love to hear your thoughts, feel free to leave a comment below. Don't forget to subscribe to my RSS Feed!