Customizing Your Powershell Prompt

by Warlock on Mar.30, 2009, under .NET, Scripting

Powershell is a great CLI for Windows and I appreciate it’s design more and more every time I use it. As I interactive with for more and more tasks, wanted to customize it a bit so that it looked prettier, and so I could visually distinguish what was going on more easily.

One thing I’ve always done in the *NIX world was add color to my prompt so that it would be easy for me to tell where each command/output sequence started and ended. To do this you need to edit (or create) the file <My Documents>\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

To determine how to render the prompt, Powershell calls a function, prompt. To change the appearance, just implement this function in your profile. Mine is as shown below:

Write-Host -nonewline "Welcome to PowerShell "
Write-Host -nonewline -foregroundcolor Magenta $env:Username
Write-Host "!"
Write-Host " "

function prompt {
            Write-Host -nonewline -foregroundcolor Magenta "PS "
            Write-Host -nonewline -foregroundcolor Green $(get-location)
            Write-Host -nonewline -foregroundcolor Magenta " >"
            " "
}

Note that the output uses the -foregroundcolor flag with Write-host to change the output color. Also note that built-in variables are used to determine context information, such as the current location.

This blog post worked a quick reference for me.


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...