Jacob Kucinic
Jacob Kucinic

Reputation: 115

Remove Color From Tee-Object Powershell

Start-Process "powershell" -ArgumentList "-noexit -executionpolicy bypass -windowstyle minimized -command `"&{Invoke-Expression `'.\$exe $Arguments`' | Tee-Object `'$Logs`'}`""

This command works, however the .exe I am running has color text, like:

[0m

Which makes it hard to parse the resulting .log file.

Is there way to Tee-Object to a file, and remove the color output?

Also, is there a way to do so, while keeping the color on the console, as the console displays the same characters (no color).

I have been searching up and down, re-wrote it a hundred different ways, and I can't seem to find a way to remove it.

Also, if there is cleaner way to write the launch besides invoke-expression | Tee-Object

It seems to be the only one that works for me.

Upvotes: 0

Views: 1062

Answers (1)

Jacob Kucinic
Jacob Kucinic

Reputation: 115

function Tee-ObjectNoColor {
    [CmdletBinding()]
    Param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        [string]$InputObject,

        [Parameter(Position=1, Mandatory=$true)]
        [string]$FilePath
    )

  process{
        $InputObject = $InputObject -replace '\\[\d+(;\d+)?m'
        $InputObject | Out-File $FilePath -Append
        $InputObject | Out-Host
         }
}

This is how I did it. It removes all color symbols, clean on both screen and log.

Upvotes: 1

Related Questions