Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a powershell script and I want to write to a console and write to a log file with one call.

I was doing this...

Start-Transcript -Path $TargetDir\Log.log
Write-Host "Stuff"

... which works great, except that the newlines it generates are LF, which means my logs look great in every text editor on earth, except for notepad.

Here's what I have for this...

function global:Write-Notepad
(
    [string] $Message,
    [string] $ForegroundColor = 'Gray'
)
{
    Write-Host "$Message`r" -ForegroundColor $ForegroundColor
}

...which writes a CR to the end of every message, but it doesn't seem to write out lines like this...

&$ACommand | Write-Notepad

I'm not sure what syntax the piping operator expects, but I would greatly appreciate help.

like image 833
user467384 Avatar asked Nov 12 '10 23:11

user467384


People also ask

How do you write a PowerShell script output to a log file?

PowerShell has a built-in transcript feature to save all commands and outputs shown in the PS console to a text log file. To log your current PowerShell session, the Start-Transcript cmdlet is used. The –Append option indicates that new sessions will be logged to the end of the file (without overwriting it).

What is the command to write something to the console display in PowerShell?

Writes the specified objects to the pipeline. If Write-Output is the last command in the pipeline, the objects are displayed in the console.

How do I print multiple lines in PowerShell?

In PowerShell multiline command can be easily created using ` (backtick character) to split long or a single line command into multiline statements. Backtick (`) character is used as an escape character, it basically escapes the newline character and results in line continuation.

What is the command to write data to a file in PowerShell?

The PowerShell way uses a single cmdlet called Set-content. This cmdlet allows us to much more easily use PowerShell to write to a file. This PowerShell cmdlet is a built-in cmdlet that has one purpose; to write to a file.


2 Answers

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.

like image 160
JasonMArcher Avatar answered Nov 11 '22 13:11

JasonMArcher


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 }
    }
}
like image 25
user467384 Avatar answered Nov 11 '22 15:11

user467384