Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

PowerShell: Change the Color of the Prompt

📅 2011-Sep-02 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ colors, powershell ⬩ 📚 Archive

 

PowerShell opens up in a soothing white-on-blue console by default. But, the prompt is displayed in the same color as the rest of the text. The color of the prompt can be changed by overriding the default Prompt method by writing a custom Prompt method and placing it in your profile script.

This is a simple Prompt method that sets the color of the prompt to yellow:

function Prompt
{
    $promptString = "PS " + $(Get-Location) + ">"
    Write-Host $promptString -NoNewline -ForegroundColor Yellow
    return " "
}

The above Prompt method is enough if the profile script is meant for the default PowerShell console only. If you are using a profile script that will be used by other PowerShell hosts (like PowerShell ISE for example), then maybe you do not want the prompt to be yellow in those hosts.

For such cases, this is a Prompt method that sets the color of the prompt to yellow only for the default PowerShell console and uses the default color for the other hosts:

function Prompt
{
    $promptString = "PS " + $(Get-Location) + ">"

    # Custom color for Windows console
    if ( $Host.Name -eq "ConsoleHost" )
    {
        Write-Host $promptString -NoNewline -ForegroundColor Yellow
    }
    # Default color for the rest
    else
    {
        Write-Host $promptString -NoNewline
    }

    return " "
}

Tried with: PowerShell 2.0


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧