Customize Your Shell, Profile, and Prompt

1. Problem

You want to customize PowerShell’s interactive experience with a personalized prompt, aliases, and more.

2. Solution

When you want to customize aspects of PowerShell, place those customizations in your personal profile script. PowerShell provides easy access to this profile script by storing its location in the $profile variable.

By default, PowerShell’s security policies prevent scripts (including your profile) from running. Once you begin writing scripts, though, you should configure this policy to something less restrictive. For information on how to configure your execution policy, see Section 16.1, “Enable Scripting Through an Execution Policy.”

To create a new profile (and overwrite one if it already exists):

        New-Item -type file -force $profile

To edit your profile:

        notepad $profile

To see your profile file:

        Get-ChildItem $profile

Once you create a profile script, you can add a function called Prompt that returns a string. PowerShell displays the output of this function as your command-line prompt.

        function Prompt
        {
            "PS [$env:COMPUTERNAME] >"
        }

This example prompt displays your computer name, and look like: PS [LEE-DESK]>

You may also find it helpful to add aliases to your profile. Aliases let you to refer to common commands by a name that you choose. Personal profile scripts let you automatically define aliases, functions, variables, or any other customizations that you might set interactively from the PowerShell prompt. Aliases are among the most common customizations, as they let you refer to PowerShell commands (and your own scripts) by a name that is easier to type.

If you want to define an alias for a command but also need to modify the parameters to that command, then define a function instead.

For example:

        Set-Alias new New-Object
        Set-Alias iexplore 'C:\Program Files\Internet Explorer\iexplore.exe'

Your changes will become effective once you save your profile and restart Power-Shell. To reload your profile immediately, run the command:

        . $profile

Functions are also very common customizations, with the most popular of those being the Prompt function.

3. Discussion

Although the Prompt function returns a simple string, you can also use the function for more complex tasks. For example, many users update their console window title (by changing the $host.UI.RawUI.WindowTitle variable) or use the Write-Host cmdlet to output the prompt in color. If your prompt function handles the screen output itself, it still needs to return a string (for example, a single space) to prevent PowerShell from using its default. If you don’t want this extra space to appear in your prompt, add an extra space at the end of your Write-Host command and return the backspace (”‘b”) character, as shown in Example 1-2.

Example 1-2. An example PowerShell prompt
function Prompt
{
    $id = 1
    $historyItem = Get-History -Count 1
    if($historyItem)
    {
       $id = $historyItem.Id + 1
    }

    Write-Host -ForegroundColor DarkGray "'n[$(Get-Location)]"
    Write-Host -NoNewLine "PS:$id > "
    $host.UI.RawUI.WindowTitle = "$(Get-Location)"

    "'b"
}

In addition to showing the current location, this prompt also shows the ID for that command in your history. This lets you locate and invoke past commands with relative ease:

        [C:\]
        PS:73 >5 * 5
        25

        [C:\]
        PS:74 >1 + 1
        2

        [C:\]
        PS:75 >Invoke-History 73
        5 * 5
        25

        [C:\]
        PS:76 >
Tags: , , , , , ,