Preserve color from piped output Preserve color from piped output powershell powershell

Preserve color from piped output


As far as I'm aware, you can't preserve the colors output by git status -s, or any other command output that is piped into PowerShell via StdIn. The color information of the text being piped into the PowerShell function via StdIn is 'lost'.

The only way I can think of adding color back in would be to perform RegEx matches or positional based coloring, using multiple Write-Host "line section" -ForegroundColor COLOR -NoNewline for each colored section.

The console uses System.IO.StreamReader to accept input via StdIn. Try this command [Console]::In | Get-Member in PowerShell/ISE.


  • Utilities (command-line programs) that support colorized output usually omit color codes when their stdout is not connected to a terminal (console), such as when output is redirected to a file or sent through the pipeline.

    • The idea is not to pollute the data with formatting instructions if the intent is programmatic processing of the output (as opposed to display in the terminal).
  • In that event you must use a utility-specific option, IF supported in order to force unconditional use of coloring.

PowerShell does pass color codes through the pipeline, so something like the following, executed from a directory with a git repository, should produce colored output.

PS> git -c color.status=always status -b --short | % { $i=0 } { "$((++$i)): $_" }1: ## master

The word master (or whatever branch is active) should appear in green.

The answer from which the -c color.status=always technique is taken was updated since you linked to it, so I suspect that an earlier form was simply not effective in forcing unconditional colored output.


If you are writing the o/p to the console[Host - to be precise]. You can use Write-Host cmdlet, which provides a "foreground" and "background" property, that you can use to set the colors of your choice.