Home > Net >  PowerShell, how to stop Tee-Object outputting additional lines?
PowerShell, how to stop Tee-Object outputting additional lines?

Time:10-15

In my script, I create a file as follows:

"Start of Log" | Out-File $ZipLog -encoding utf8 -force -ea silent

Then for each entry, I want date, then a few lines of information:

Add-Content $ZipLog "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")"
'Application installer:' | Tee-Object $ZipLog -Append
'More details 11 ...' | Tee-Object $ZipLog -Append
'More details 22 ...' | Tee-Object $ZipLog -Append
'More details 33 ...' | Tee-Object $ZipLog -Append

However, the output is then like this:

2022-10-14 16:30:05
Application installer:

More details 11 ...

More details 22 ...

More details 33 ...

Why is Tee-Object introducing additional line breaks after every use in the output log file (while Add-Content is not doing this)?

CodePudding user response:

tee-object basically uses out-file. out-file can mix encodings when appending. I would prefer add-content which checks the current encoding of the file. The last 4 lines are in utf16. I would never use out-file -append, tee-object -append, or '>>', for risk of file corruption.

'Application installer:' | add-content $ziplog -passthru
'More details 11 ...'    | add-content $ziplog -passthru
'More details 22 ...'    | add-content $ziplog -passthru
'More details 33 ...'    | add-content $ziplog -passthru
  • Related