11

We have build scripts for cmd. For example here is output of one of them.

Colored output of build script from cmd

When I run same commands in PS it outputs everything in red (probably because it's for err output).

Red output of powershell

Can I keep colors somehow?

xvorsx
  • 2,322
  • 2
  • 18
  • 19

2 Answers2

4

At first, I didn't think it was possible.

But, after a little searching I found this blog post, Colorized capture of console screen in HTML and RTF.

You should be able to use the PowerShell script from there to create what you need.

nvidot
  • 1,262
  • 1
  • 7
  • 20
aphoria
  • 19,796
  • 7
  • 64
  • 73
  • Nice! Still need some work to do, but it looks like I can create a wrapper that preserve colors. – xvorsx Sep 19 '13 at 02:37
  • After trying a couple of small examples, I can't reproduce the behavior you're seeing. If you can provide a simple repro, I'll dig further for you. – Jason Shirk Sep 26 '13 at 04:14
  • @JasonShirk What can't you reproduce? – aphoria Sep 26 '13 at 14:30
  • 1
    With a trivial C# console app, write colored output to both stdout and stderr. Call this exe directly from PowerShell and from a bat file called from PowerShell. Both ways colors look the same as if called from cmd. – Jason Shirk Sep 26 '13 at 14:51
  • 1
    @JasonShirk I'm not following what you're saying. xvorsk wants to capture the colors of the output, not just the text of the output. – aphoria Sep 26 '13 at 15:37
  • 1
    @aphoria The output turned red when run from PowerShell. I can't reproduce that. – Jason Shirk Sep 26 '13 at 17:15
  • 1
    Actually, I think _I've_ misunderstood the question. I thought he wanted to capture the colors of the outputted text. Really he is asking why the colors do not show up when called from PowerShell. – aphoria Sep 27 '13 at 15:04
2

I'm not positive this will work, but if you don't mind redirecting stderr to stdout, PowerShell won't try to color everything red. To do this, just add 2>&1 when you run your cmd script, e.g.

build.cmd 2>&1
Jason Shirk
  • 7,734
  • 2
  • 24
  • 29
  • Thank you, I already tried it - doesn't work for me. Looks like we have a lot of subscripts/exe/powershell subcalls and 2>&1 doesn't work for nested calls. – xvorsx Sep 21 '13 at 08:15
  • 2
    Just surround it with quotes to pass the redirection to cmd.exe: `"2>&1"` – wOxxOm Aug 27 '16 at 11:59