views:

388

answers:

7

I'm looking for different examples of custom Powershell prompt function implementations. If you have a custom implementation of your own please post the script. Links to existing resources are good as well.

Bonus points for posting a screenshot of what your prompt actually looks like (a preview).

+4  A: 

Here is my prompt function

function prompt() {
    if ( Test-Wow64 ) {
        write-host -NoNewLine "Wow64 "
    }
    if ( Test-Admin ) { 
        write-host -NoNewLine -f red "Admin "
    }
    write-host -NoNewLine -ForegroundColor Green $(get-location)
    foreach ( $entry in (get-location -stack)) {
     write-host -NoNewLine -ForegroundColor Red '+';
    }
    write-host -NoNewLine -ForegroundColor Green '>'
    ' '
}
JaredPar
`Write-Host -NoNewLine -Fore Red ('+' * (Get-Location -Stack).Count)`? :-)
Joey
@Johannes, that adds a series of +'s to the prompt indicating how deep in the push stack I am
JaredPar
I know, I just eliminated the foreach, since you actually don't need every individual item but just the number of items.
Joey
@Johannes, ah gotcha. Need more coffee
JaredPar
A: 

I tend to re-type

function prompt { "PS> " }

every time I am preparing examples I can copy/paste to someone, especially when I'm in cumbersome long paths which would only distract.

And I still plan to write a decent prompt function which shows me the drive and a useful approximation on the location by either using the current directory (without the path that led to there) or (if it's numeric) the next higher level too. But that's probably pretty specific to my own filesystem here. And I never was bothered enough by the default prompt to actually do it :-)

Joey
+3  A: 
stej
The idea of putting task breadcrumbs on the prompt is very interesting. I may incorporate that into my prompt. ... Looks like a typo on the first Write-Host. The 'Newline' should be '-NoNewline' as in the following: Write-Host "$($toPrompt) >" -ForegroundColor Green -NoNewline }
totorocat
Thx, corrected ;)
stej
+1  A: 

Here's mine: http://winterdom.com/2008/08/mypowershellprompt

tomasr
+1  A: 

i often use posh as a calc, so i set $ans variable. https://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=386493

PS > 100
100
PS > $ans * 9
900
PS > $ans*$ans
810000

A: 

Here's mine. Just has the history ID at each command so I can easily identify the ID of the command. I also use the windowtitle to give me the current working directory rather than have it displayed in the prompt itself.

106 >  cat function:\prompt

    $history = @(get-history)
    if($history.Count -gt 0)
{
        $lastItem = $history[$history.Count - 1]
        $lastId = $lastItem.Id
    }

    $nextCommand = $lastId + 1
    $Host.ui.rawui.windowtitle = "PS " + $(get-location)
    $myPrompt = "$nextCommand > "
    if ($NestedPromptLevel -gt 0) {$arrows = ">"*$NestedPromptLevel; $myPrompt = "PS-nested $arrows"}
    Write-Host ($myPrompt) -nonewline
    return " "

One thing that many people forget is to deal with in custom prompts is the nested prompt. Note that I check $nestedPromptLevel and add an arrow for each nested level.

Andy

Andy Schneider