powershellcommand-promptpromptcmd

Color for the PROMPT (just the PROMPT proper) in cmd.exe and PowerShell?


So in Bash you just configure PS1 to add colors to your prompt. I'm talking about the prompt proper, not the color of the foreground (text) or the background. And it's really easy in Bash and it helps a lot if you need to find your commands in a sea of messy text output.

Can you achieve the same for cmd.exe, or as a fallback, for PowerShell? A colored prompt?

I don't know if it could be done in the old days before Win32 by loading ANSI.SYS. I think that was just to make the foreground and the background colorful. But I might be wrong. And anyway, those days are gone, and in our modern times (I know), we're using cmd.exe, or PowerShell.

I know both cmd.exe and PowerShell are capable of colored output. For cmd.exe, just run color /? to find out. But my question is not about the foreground and the background, that's all known to humankind - it's about just changing the prompt color for cmd.exe, probably via the PROMPT environment variable as via the PS1 variable for Bash? Is it possible?

And no, Cygwin is not an alternative for this. I'm a Cygwin user with MinTTY and all, and I love it. But I still want my cmd.exe prompt colored, too.


Solution

  • Building on @KriZ's answer, the ANSI escape sequences work perfectly in Windows 10 cmd.exe as of 2019. Didn't need to explicitly call out ansi.sys or copy any files. It just worked out of the box in Windows 10.

    For example,

    set PROMPT=$E[1;37m[user@machine:$E[1;35m$P ]$$ $E[1;37m
    

    Produces:

    example colored prompt

    (Notice the space after the final $)

    Everything before the drive is colored in bold white and the drive/folder is bold pink, and everything after the final $ is bold white.

    The format for the colors is:

    $E[bold_or_not;colorm
    

    With m always following the color number. bold_or_not = 0 or 1. Here's a guide for the colors:

    0     Turn Off Attributes
    1     High Intensity
    2     Normal Intensity
    4     Underline (mono only)
    5     Blink
    7     Reverse Video
    8     Invisible
    30    Black
    31    Red
    32    Green
    33    Yellow
    34    Blue
    35    Magenta
    36    Cyan
    37    White
    40    Black
    41    Red
    42    Green
    43    Yellow
    44    Blue
    45    Magenta
    46    Cyan
    47    White
    

    Colors Source: https://kb.iu.edu/d/aamm