3

The question seems kinda basic, but I have not been able to find it online. I am creating log files, and upon finding different things, I want it to be with color. Green is OK, red is bad. Just to make it faster to go through. However, whatever I seem to to do with Out-File, Add-Content or whatever I`m not able to get it going.

Anyone got a clue?

$stringRed = "This text should be red"
$stringGreen =  "This text should be green"
$Logfile = "C:\text.txt"

Function LogWrite
{
   Param ([string]$logstring)

   $logstring | Out-File -FilePath $Logfile -Append
}

 LogWrite $stringRed -ForegroundColor  Red 
 LogWrite $stringGreen -ForegroundColor  Green

 #writes corretly to the console
 Write-Host $stringRed -ForegroundColor Red 
 Write-Host $stringGreen -ForegroundColor  Green
bananajoe19
  • 31
  • 1
  • 3
  • 5
    TXT doesn't support colors, you need RTF or DOC or HTML or anything that does support it – Andrey Marchuk Apr 28 '16 at 09:31
  • 1
    Not sure if it helps, but I was in the same situation. I am logging to screen to show progress, and errors are in Red, warnings in Blue, etc. To address it in TXT log files I just prepended !! in front of errors and ** in front of warnings, with spaces in front of the rest. It creates a column of white space on the left, where you can quickly see the prefixes and find what you need in the log. – Gordon Apr 28 '16 at 10:15

0 Answers0