Minor PowerShell Prompt Customization

By on 7/14/2011

Having recently started using PowerShell a lot more, I wanted to share a minor customization I made to my powershell prompt.
function prompt
{
    $loc = $(get-location).ToString()
    $usr = ($env:userprofile).ToString()
    if ($loc.StartsWith($usr, $true,[System.Globalization.CultureInfo].CurrentCulture)) {
	$loc = "~"+$loc.SubString($usr.Length, $loc.Length - $usr.Length)
    }
    Write-Host ("PS " + $loc +">") -nonewline -foregroundcolor Yellow
    return " "
}
all it does is that when I'm in my user profile directory, it shortens the prompt to something like: PS ~>. Since you can use the '~' character as a shortcut for your user directory, may as well shorten the path to give you more space. Also, it makes the prompt yellow so that it can be more easily distinguished from the rest of the output.

See more in the archives