19

I always make my prompt to be this color in my linux Then I can find my input in the black command window. But can we make the same color in my Powershell?

  • What is the console font? – Dmitry Kudriavtsev Oct 17 '17 at 19:16
  • @DmitryKudriavtsev Look this, maybe it is Sim song in English. – yode Oct 17 '17 at 19:36
  • This is SimSun maybe? – Dmitry Kudriavtsev Oct 17 '17 at 19:37
  • @DmitryKudriavtsev Yes, sorry I make a mistake. – yode Oct 17 '17 at 19:38
  • not your fault, transliteration is hard – Dmitry Kudriavtsev Oct 17 '17 at 19:39
33

screenshot of coloured prompt

PowerShell prompts

What you want to do is customise the prompt.

The default prompt in PowerShell 3.0 and newer is:

function prompt  
{  
    "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "  
}

Colour escape sequences

Now, Windows 10 supports the ANSI escape codes in its conhost, and 24-bit colour is supported since 1703.

To use one of the ANSI escape codes, we need a literal ESC character. This is octal 033, or decimal 27. In Bash, you would use "\033" or "\e"; There's no direct equivalent sequence in PowerShell but you can instead embed an expression: "$([char]27)"

In PowerShell Core you can use the "`e" escape sequence instead. Thanks to the comment by asherber.


Use in PowerShell

This means you can, for example, use the following escape sequence to set the background colour to cyan with the code 46:

"$([char]27)[46m"

Which can be used like so:

echo "$([char]27)[46mColoured text"

To make things more readable, let's define a variable:

$ESC = [char]27

So we can use:

"$ESC[46m"

Where $ESC is the literal ESC character, [ defines the start of the param list, and m the end.

Using it in a command:

echo "$ESC[46mColoured text"

And to restore the original colour settings, we use the code 0 instead:

echo "$ESC[46mColoured text$ESC[0mUncoloured text"

More advanced colours

If you aren't happy with the basic 16-colour palette, you can use full 24-bit colours with the form:

"$ESC[48;2;<r>;<g>;<b>m"

Where <r> is the decimal value for red from 0-255, <g> for green and <b> for blue, e.g. "$ESC[48;2;255;0;123m"

Please refer to the documentation for more information.

In fact, this is all directly equivalent to what you would do in bash, except you need to use $ESC (after you've defined it) instead of \e or \033.


Setting a coloured prompt

Putting these together, we can use the ANSI colour escape codes in a modified prompt to change the background colour:

function prompt  
{  
    $ESC = [char]27
    "$ESC[46mPS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) $ESC[0m"  
}

Persistence

If you want this to persist across sessions, add it to your profile (like .bashrc). You can view the path to your profile in $profile.

| improve this answer | |
  • Hi~,it is a great answer..But further more, could we make the text be bold style?Then we can find it more easy.. – yode Oct 17 '17 at 14:51
  • 1
    @yode It's more or less all documented here, which I linked in the answer ;) – Bob Oct 17 '17 at 15:14
  • 1
    @yode Ah. If you compare to 37 non-bright white, it looks like PowerShell windows already use 97 bright white by default. (btw, there's going to be a new colour palette for clean installs of the next Win10 version. There's some screenshots there of the current palette; note that 1;37m is the same as 97m, both meaning "bright white") – Bob Oct 18 '17 at 3:41
  • 1
    @yode Basically, conhost does not yet support bold. Bright is a substitute for bold, and you're already using the bright colour by default. If you really want bold, you can use an alternative terminal, such as ConEmu — but it looks like their support for ANSI bold isn't complete either. IMO, it isn't worth the trouble. – Bob Oct 18 '17 at 3:47
  • 1
    I can't get the Markdown to work correctly, but in PowerShell Core you can use backtick+e for [char]27. I'm not sure if this was added in earlier versions. – asherber Dec 18 '19 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.