Preserve color from piped output
<p>I have this extremely simple powershell script that prepends each line with a number.</p>
<pre><code>function number
{    
    $i = 0;
    foreach($line in $input)
    {
        [string]$i + &quot;:&quot; + $line
        ++$i
    }
}
</code></pre>
<p>I would like this function to preserve the colors of the input. For example if I execute the following command the colors are lost:</p>
<pre><code>git status -s | number
</code></pre>
<p>While executing</p>
<pre><code>git status -s
</code></pre>
<p>Gives me a nicely colored output. What can I change in my powershell function to accomplish this.</p>
<p>Note I've already tried telling git to always output colors with the information in the answers to <a href="https://stackoverflow.com/questions/16073708/force-git-status-to-output-color-on-the-terminal-inside-a-script">this question</a> so I think its my side that is ignoring the colors.</p>
<p><em>This is not a duplicate of <a href="https://stackoverflow.com/q/18861709/45375">the question this one was previously marked a duplicate of</a>.</em></p>
<p>I've seen that question, which is actually about how to prevent MSBuild from writing to standard error instead of standard out, which prevented the colors from showing up in that case. The only thing similar about that question and this one is the title. The other answer there (with HTML conversion) does not apply here as that requires the data is written to the console and not piped.</p>