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

6

26

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.

Amann answered 9/6, 2011 at 17:47 Comment(0)
G
14

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

Getup answered 28/3, 2019 at 18:52 Comment(3)
One note is that the $E[x;y;zm seems to support more than two items. There may be a little more nuance to being able to select colors than I figured out for my post above. – Getup
PROMPT $E[1;36m[%username%@%computername%]:$E[1;35m$P$G$S$E[1;37m – Ducks
For πŸ‡©πŸ‡ͺ German date and time as 31.10 12:30, removing excessive precision with $h, plus folder: $E[1;37m::$s$E[1;35m$d$h$h$h$h$h$E[0;36m$s$t$h$h$h$h$h$h$E[1;37m$s$p$E[0;37m$s – Amann
A
17

You can add a Prompt function to your profile in Powershell to pretty much do whatever you want with the prompt. So for instance something like this:

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

To open or create your PowerShell profile, run this:

if(Test-Path $profile){notepad $profile}else{New-Item -path $profile -type file -force}
Apuleius answered 9/6, 2011 at 18:1 Comment(2)
And you should return at least space. Otherwise PowerShell will append 'PS>' at the end. – Glavin
Thanks - works nicely, after doing Set-ExecutionPolicy RemoteSigned (for simplicity's sake). But without having done much searching for how it was possible on PowerShell I was sure that it was possible. The ANSI hack for cmd.exe, on the other hand, that's sensational! :-) – Amann
T
14

follow this link. There's an ANSI hack developped for the CMD.exe shell

link to ansi hack

I've tried it on my win 7 professional SP1 and works like a charm

enter image description here

Thirtieth answered 9/6, 2011 at 20:26 Comment(5)
Phantastic, thanks! I got it working, too. The instructions in the readme.txt are good, just one thing missing: On 64 bit systems, you have to copy the two DLLs - AnsiSupport.dll and DllSpoof.dll - to the C:\Windows\SysWOW64 folder instead of the C:\Windows\system32 folder. (Be sure to read up on the folder trickery unless you know!) This was the case on my system - Win7 Pro 64 bit. Working fine and colorful now - thanks again, that's great! – Amann
CAVEAT: This is not a 1:1 replacement plus ANSI colors. Although there's no problem most of the time, some things may well stop working. One example I've come across is the powercfg.exe utility. The 32 bit ANSI hack will pick up stuff in C:\Windows\SysWOW64 (32 bit), the original one stuff in C:\Windows\System32 (64 bit). With powercfg, this does make a big difference. The 32 bit version wouldn't tell me why my computer woke up. Definitely keep this in mind lest you get bitten. – Amann
what are these non-standard two additional buttons on the top right corner of the cmd.exe window ? – Margo
These are additional buttons from Ultramon. Bought that licensed version a couple years ago and can't live without it anymore. The buttons allow any window (including cmd) to either be stretched or moved to the other screen by clicking on them. That combined with 'the each screen has its own taskbar' makes it a must have for dual or triple screen people. – Thirtieth
On Win 10 ansi colors work out of the box without any hacks – Inexpedient
G
14

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

Getup answered 28/3, 2019 at 18:52 Comment(3)
One note is that the $E[x;y;zm seems to support more than two items. There may be a little more nuance to being able to select colors than I figured out for my post above. – Getup
PROMPT $E[1;36m[%username%@%computername%]:$E[1;35m$P$G$S$E[1;37m – Ducks
For πŸ‡©πŸ‡ͺ German date and time as 31.10 12:30, removing excessive precision with $h, plus folder: $E[1;37m::$s$E[1;35m$d$h$h$h$h$h$E[0;36m$s$t$h$h$h$h$h$h$E[1;37m$s$p$E[0;37m$s – Amann
L
6

This is all good information but an important thing that I didn't see addressed is how to make the custom prompt appear each time you run a command prompt. In older Windows, such as XP and before, you would put the PROMPT environment variable in the AUTOEXEC.BAT file but in Windows 7 through Windows 10, you would make it permanent as follows:

  • Open the Run prompt by using the Windows key + R
  • Type "systempropertiesadvanced" (without the quotes) and hit ENTER
  • This will open the System Properties dialog box (You can also right click My Computer and choose Properties to get this)
  • Select the "Advanced" Tab at the top
  • Choose "Environment Variables" near the bottom
  • In the lower area, in the "System variables" area, look and see if you currently have a variable called "Prompt" (capitalization doesn't matter)
  • If so, edit the prompt variable and your changes with be permanent
  • If not, click "New" near the bottom and for Variable name, enter PROMPT and for the variable value, whatever you want it to be. The default prompt has a variable value of $P$G
  • Click OK
  • Run the command prompt to test
  • DONE
  • Note: I use a custom command prompt which looks like the Texas flag. The Variable value for this is: $e[1;44m*$e[41mβ–€β–€$e[0;1m $P$G

(The white bar is made by holding down ALT and typing 223 on the keypad on the right. There are two of these characters in this prompt.)

Labile answered 13/7, 2016 at 21:10 Comment(1)
Okay that's brilliant. – Armstead
V
2

You can use multiple colors (very useful for identifying components of your prompt, typical in Unix):

function prompt {
    Write-Host ("@") -NoNewLine -ForegroundColor Magenta
    Write-Host ("$env:COMPUTERNAME") -NoNewLine -ForegroundColor Green
    Write-Host (":") -NoNewLine -ForegroundColor Magenta
    Write-Host ($(Get-Location)) -NoNewLine -ForegroundColor Green
    Write-Host (">") -NoNewLine -ForegroundColor Red
    return " "
}

enter image description here

(COMPUTERNAME was explicitly written here, but it actually gets replaced by the value of the environment variable).

And you can add random colors (taken from here; this has a similar version; both have other very interesting tweaks):

function prompt
{
    $random = new-object random
    $color=[System.ConsoleColor]$random.next(1,16)
    Write-Host ("PS " + $(get-location) +">") -nonewline -foregroundcolor $color
    return " "
}
Valeric answered 18/12, 2013 at 19:32 Comment(0)
A
0

Thanks to all existing answers, this is my command prompt (if you like it, you can use it)

$E[7;33m$P$_$E[0;37m[$E[0;32m%username%@%computername%$E[0;36m$S$D$S$T$H$H$H$E[0;37m]$$$E[0;37m$S

Just need to add a User Variable PROMPT with the above value

Output:

enter image description here

Audiphone answered 23/12, 2022 at 7:27 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.