Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use various Linux programs on my Windows machine; some of them have colorized output. GnuWin32 and so forth.

However, Windows PowerShell does not support Linux colors; it gets a message like ←[0m31m(which is the color control code), and renders that instead of the color.

Is there any way around this?

share|improve this question
up vote 3 down vote accepted

Powershell is just a text-mode program, running in the Windows console. The Windows console does not support ANSI escape sequences which is what you are seeing.

To fix it, it depends a lot on which program you are using. I am guessing you use cygwin to run these scripts?

share|improve this answer
In this instance, I was using a python program, mercurial. – Paul Nathan Dec 18 '09 at 19:03
I wish you had said Perl, then I would tell you about the Win32::Console module for Perl :) I am not sure what you can do to get colors with Windows-Python. – Kevin Panko Dec 18 '09 at 20:40

You can use Cygwin to run PowerShell and PowerShell scripts:

alt text

Cygwin can handle the escape sequences.

share|improve this answer
What if I don't want to use cygwin? – Paul Nathan Dec 18 '09 at 19:03
Then you can take any of the other suggestions here – Daniel H Mar 29 '10 at 17:57

For posterity

You can use ANSICON to display ANSI escape sequences in Windows consoles, including Powershell. This should give you color.

This issue thread on getting it to work with PowerShell has a few different solutions as there is more than one way to do it.

share|improve this answer

I was also searching for a good solution, but I couldn't find any ...

Here is a short PowerShell script to translate ANSI color codes:

function Open-Colored([String] $Filename)
  { Write-Colored(cat -Raw $Filename) }

function Write-Colored([String] $text)
  { # split text at ESC-char
    $split = $text.Split([char] 27)
    foreach ($line in $split)
      { if ($line[0] -ne '[')
          { Write-Host $line -NoNewline }
          { if     (($line[1] -eq '0') -and ($line[2] -eq 'm')) { Write-Host $line.Substring(3) -NoNewline }
            elseif (($line[1] -eq '3') -and ($line[3] -eq 'm'))
              { # normal color codes
                if     ($line[2] -eq '0') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor Black       }
                elseif ($line[2] -eq '1') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkRed     }
                elseif ($line[2] -eq '2') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkGreen   }
                elseif ($line[2] -eq '3') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkYellow  }
                elseif ($line[2] -eq '4') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkBlue    }
                elseif ($line[2] -eq '5') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkMagenta }
                elseif ($line[2] -eq '6') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor DarkCyan    }
                elseif ($line[2] -eq '7') { Write-Host $line.Substring(4) -NoNewline -ForegroundColor Gray        }
            elseif (($line[1] -eq '3') -and ($line[3] -eq ';') -and ($line[5] -eq 'm'))
              { # bright color codes
                if     ($line[2] -eq '0') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor DarkGray    }
                elseif ($line[2] -eq '1') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Red         }
                elseif ($line[2] -eq '2') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Gree        }
                elseif ($line[2] -eq '3') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Yellow      }
                elseif ($line[2] -eq '4') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Blue        }
                elseif ($line[2] -eq '5') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Magenta     }
                elseif ($line[2] -eq '6') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor Cyan        }
                elseif ($line[2] -eq '7') { Write-Host $line.Substring(6) -NoNewline -ForegroundColor White       }


Open-Colored .\myColoredLogfile.log

Currently, I have no idea how to integrate this into a pages (e.g. less) or how to write a own simple pager like less.

PSCX comes with a less clone, but it has no ANSI color code support (yet)...

share|improve this answer

You could use a Linux virtual machine to launch these scripts. This is a big thing for a small result if it's just about showing colors, but it can't hurt to run the scripts on the platform they were made for.

share|improve this answer
yeah, that's too big to justify for this one thing. Plus I don't have a VM hypervisor running, and I do not want to install one. – Paul Nathan Dec 18 '09 at 16:49

You said you were using Mercurial to run the Linux programs. Assuming you're referring to the source control system (which I didn't realize could run Linux programs; I only use it for source control), there's an extension that will let you show color. Wincolor will intercept the ANSI color control and call the appropriate Windows functions. It seems to work no matter what, but I think it works better with pyreadline installed.

Note: I have treid this without pyreadline, and it didn't work for me. I have not yet tried it with pyreadline.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .