title: I would like to color highlight sections of a pipeline string according to a regex in Powershell
body: <p>This is a question of technique, but as an exercise my intention is to write a PS to accept piped input, with a regex as a parameter, and highlight any text matching the regex.</p>
  
  <p>The part I'm not able to find any info on is that it's easy to match text, capturing to a buffer, or to replace text.  But I need to replace matched text with color control, the original text, then resume the previous color.  I can't seem to find any way to generate color output other than with write-output, and can't do separate colors in a single write, which would mean:</p>
  
  <p>-matching the regex</p>
  
  <p>-write-host out all text prior to the match in default color, with -NoNewLine</p>
  
  <p>-write-host the match, with -NoNewLine</p>
  
  <p>-write-host the remainder</p>
  
  <p>This seems messy, and gets even more messy if we want to support multiple matches.
  Is there a more eloquent way to do this?</p>