Everybody has their own idea of what the perfect prompt in PowerShell consists of. My prompt is pretty boring, but it’s got one pretty unique feature – my prompt function doubles as a useful command!
You see, a lot of times I wind up in deep directories of solution and project folders in Visual Studio and I wind up with a prompt that barely fits on one line, or even worse, wraps. In this cases, I often only care about having a vague idea of where I am.
# sometimes I only want a little information about where I am. C:\Users\Josh Einstein\Documents\Visual Studio\Projects> Prompt -Minimize # sometimes I don’t care at all PS Projects> Prompt -Hide # if I need to bring it back to the way it was, I can just do this. PS> Prompt -Show C:\Users\Josh Einstein\Documents\Visual Studio\Projects>
This has really come in handy for me and it’s something you could easily do with a separate script or function in your profile. But I figured, hey the prompt function is already there right? What’s it gonna hurt if I add a few parameters to it?
############################################################################## #.SYNOPSIS # The all-important Prompt function called by PowerShell in order to display # the prompt, with an added twist using the built-in parameters. # #.DESCRIPTION # Customizing the prompt is common in PowerShell, but this function adds some # parameters to the function that enable some dynamic behavior. These # parameters will not affect the way PowerShell calls the function, but they # allow you to minimize or hide the prompt by passing switches. # #.PARAMETER Minimize # Shrinks the prompt so that it shows only the leaf portion of the current # provider path. Restore it using the -Show parameter. # #.PARAMETER Hide # Sets the prompt to an ultra-compact mode, displaying only an indicator, # and no information about the current location. # #.PARAMETER Show # Restores the prompt to it’s default mode which displays the full location # of the current provider path. # #.LINK # about_prompts # #.EXAMPLE # Prompt -Minimize # Prompt -Hide # Prompt -Show ############################################################################## function Prompt { [CmdletBinding(DefaultParameterSetName='Prompt')] param ( [Parameter(ParameterSetName='Minimize')] [Switch] $Minimize, [Parameter(ParameterSetName='Hide')] [Switch] $Hide, [Parameter(ParameterSetName='Show')] [Switch] $Show ) switch ( $PSCmdlet.ParameterSetName ) { Minimize { $Global:PromptVisibility = ‘Minimized’ } Hide { $Global:PromptVisibility = ‘Hidden’ } Show { $Global:PromptVisibility = ‘Default’ } Prompt { switch ( $PromptVisibility ) { Minimized { “PS $(Split-Path $PWD -Leaf)> ” } Hidden { “PS> ” } Default { “$PWD> ” } } } } }
Posts