Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 418
Lumi Avatar asked Jun 09 '11 17:06

Lumi


People also ask

What is the color command in CMD?

Color is an inbuilt command found inside the Windows Command Processor (cmd.exe), that is used for changing the colors for the console's foreground and background. By default, the console has white foreground color and black background color (07 color code).

How do I change my prompt color?

To set the default Command Prompt window color, select the upper-left corner of the Command Prompt window, select Defaults, select the Colors tab, and then select the colors that you want to use for the Screen Text and Screen Background.

How do I permanently color my Command Prompt?

Start a cmd.exe. Right click on the title bar. select either defaults or properties . Next select the tab [Colors], change to the desired colour and press [OK]

How do I customize a PowerShell prompt?

To customize the prompt, write a new Prompt function. The function is not protected, so you can overwrite it. Then, between the braces, enter the commands or the string that creates your prompt. The following Prompt function displays the history ID of the next command.


2 Answers

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} 
like image 142
EBGreen Avatar answered Sep 21 '22 05:09

EBGreen


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

like image 35
KriZ Avatar answered Sep 20 '22 05:09

KriZ