Search code examples
powershellselect-string

PowerShell Select-String causing Esc characters


I am trying to combine some logs together and select only the lines that have a high underrun (u:). Anything over 0 is high. The code works wonders inside the terminal. However, when I push the information to a file, I get ESC characters instead of the information.

$Files = get-childitem C:\Tmp
$Underruns = foreach ($file in $Files) {
    $File | get-content | Select-String -Pattern ",u:[1-9].*,o:"
}
$Underruns | Out-File -Encoding utf8BOM -FilePath c:\tmp\combined.txt

The code above is adding escape characters. These characters are stopping the flow of the code. How can I prevent this from happening?

(j:0[7m,u:15424,o:[0m0)

Thank you for your help.


Solution

  • After some digging the solution to this is the -Raw flag on select-string.

    $Files = get-childitem C:\Tmp
    $Underruns = foreach ($file in $Files) {
        $File | get-content | Select-String -Pattern ',u:[1-9].*,o:' -Raw
    }
    $Underruns | Out-File -Encoding utf8BOM -FilePath c:\tmp\combined2.txt