Customizing the PowerShell command prompt II

I just picked up a copy of Windows PowerShell Cookbook by Lee Holmes. One of the first examples in the book is customizing the PowerShell command prompt. His example sets the command window title as part of the prompt function. For example, adding

$host.UI.RawUI.WindowTitle = "$env:computername $pwd.path"

to the function given in my previous post would display the computer name and full path to the working directory in the title bar. The full code would be

function prompt
    $m = 30 # maximum prompt length
    $str = $pwd.Path
    if ($str.length -ge $m)
        # The prompt will begin with "...",
        # end with ">", and in between contain
        # as many of the path characters as will fit,
        # reading from the end of the path.
        $str = "..." + $str.substring($str.length - $m + 4)
    $host.UI.RawUI.WindowTitle = "$env:computername $pwd.path"
    "$str> "

Comments are closed.