15

Is there any way to stop PowerShell from removing console message colors when using tee-object?

When I run without tee-object I get the nice error and verbose powershell message colors like this:

powershell.exe -noprofile -file $project_root/test_main.ps1

with color

However, when I'm using tee-object (b/c I want logging to console & file), the message colors are not shown on the console (I know the file won't show it) like this:

powershell.exe -noprofile -file $project_root/test_main.ps1 | tee-object -FilePath $log

no color

If powershell is just using tee-object to split the output to a file in addition to the console, why am I losing the console formatting?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
DetectiveEric
  • 251
  • 2
  • 12

1 Answers1

5

Try this instead:

powershell.exe -noprofile -command { $path\test_main.ps1 | tee-object $log }

This happens because this part is executed first:

powershell.exe -noprofile -file $project_root/test_main.ps1 

Such that what tee-object sees is the output of a native EXE. And AFAICT, PowerShell doesn't output error records (or highlight) stderr output from a native EXE (unless you redirect the error stream e.g. 2>err.log.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • I see what you're saying; however, I can't get that command method to work. First error was "You must provide a value expression on the right-hand side of the '/' operator." I tried escaping it with no luck. Then I tried making the "$path\test_main.ps1 | tee-object $log" a $command var, so I call powershell.exe -noprofile -command { $command } but that's not even calling the script or throwing an error when I call a non-existant script. As you can see Keith, I'm working off of your blog post "Effective PowerShell Item 14: Capturing All Output from a Script" to capture & log all output :) – DetectiveEric Jul 22 '10 at 14:47
  • If you run the command from PowerShell.exe it should work (at least it did for me on my test script). If you're running it somewhere else besides PowerShell use `... -command "&{ $path\test_main.ps1 | tee-object $log }"`. – Keith Hill Jul 22 '10 at 15:03
  • RE "capturing all output from a script" - I was disappointed when PowerShell 2.0 shipped without addressing the script logging issue. :-( – Keith Hill Jul 22 '10 at 15:05