Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve coloring when using Powershell's Out-Host

I have a command-line executable that prints colored text to standard out. When I do the following...

my_executable.exe | Out-Host

...I find that Out-Host removes all colored output. This is a problem when I want to display the colored output of the program without sending it through the pipeline.

Is there another way that allows me to display the colored output of console programs/batch files without sending it through the pipeline?

EDIT:

In other words, what I want is this:

Output with coloring

But Out-Host causes this:

Output without coloring

EDIT 2:

Here's why I can't just call my_executable.exe:

I have a script in a .ps1 file similar to the following

param($someValue)

# do some things
.\my_executable.exe | Out-Host 
# do some more things
return 1

Now when I do the following:

$result = .\my-script.ps1

$result is 1. If I don't use Out-Host, $result will be the output of my_executable.exe with a 1 at the end. Out-Host allows me to display the output of my_executable.exe without returning it to the caller. What I want is a way to display the colored output of my_executable.exe without returning it to the caller.

like image 301
Phil Avatar asked Jun 27 '12 20:06

Phil


1 Answers

$exe = ".\my_executable.exe"
$args = ""
$process = Start-Process $exe $args -NoNewWindow -Wait -ErrorAction Stop -PassThru
if ($process.ExitCode -ne 0) {
    throw "FAILED: $exe $args"
}
like image 111
Trey Mack Avatar answered Nov 07 '22 12:11

Trey Mack