How do I write to the console and a log file in one call in Powershell with CR LF newlines How do I write to the console and a log file in one call in Powershell with CR LF newlines powershell powershell

How do I write to the console and a log file in one call in Powershell with CR LF newlines


Try this:

& $ACommand | Tee-Object -FilePath $TargetDir\Log.log | Write-Host

Tee-Object will send a copy of the pipeline objects to a file or a variable, and output at the same time.


Here's the solution I settled on...

# This method adds a CR character before the newline that Write-Host generates.# This is necesary, because notepad is the only text editor in the world that# doesn't recognize LF newlines, but needs CR LF newlines.function global:Write-Notepad(    [string] $Message,    [string] $ForegroundColor = 'Gray'){    Process    {        if($_){ Write-Host "$_`r" }    }    End    {        if($Message){ Write-Host "$Message`r" -ForegroundColor $ForegroundColor }    }}