1

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)?

YorSubs
  • 3,194
  • 7
  • 37
  • 60
  • Is this Windows PowerShell or PowerShell Core? – Santiago Squarzon Oct 14 '22 at 14:42
  • PowerShell 5.1. I'm currently reading this answer, and it appears to me that `Tee-Object` is an unbelievably broken cmdlet ... i.e. why can't things just output in a *normal* way, or at the very least, allow me to set the encoding? ... https://stackoverflow.com/questions/74070870/powershell-how-to-stop-tee-object-outputting-additional-lines – YorSubs Oct 14 '22 at 14:44
  • Indeed, it breaks for me in Windows PowerShell, in PowerShell Core works fine tho (good reason to start using it ;)) – Santiago Squarzon Oct 14 '22 at 14:45
  • On reading that other answer, I'm even more confused about: *What is a clean and efficient way to output to screen and to a text file at the same time?* If anyone has an answer for this, I would greatly appreciate knowing. I see an option on `tee -var a` then outputting that, but then there is a whole discussion about why this is not good. – YorSubs Oct 14 '22 at 14:45
  • Ah, I put the wrong answer link in the above comment, but it's locked so I can't edit it. The answer I am referring to is this: https://stackoverflow.com/questions/58919092/why-does-powershells-tee-object-mess-up-the-encoding-of-my-file – YorSubs Oct 14 '22 at 14:51
  • Use `Out-File $ZipLog -encoding unicode...` to match the same encoding as `Tee-Object` is using – Santiago Squarzon Oct 14 '22 at 14:52
  • What if I can't use unicode, i.e. what if I am writing to a file that is already created as UTF8 so I do not have that option (as will inevitably happen)? It seems such an inelegant hack to force us into using unicode just because `Tee-Object` is broken ... – YorSubs Oct 14 '22 at 14:54
  • 1
    Well that's Windows PowerShell for you unfortunately, there are workarounds for sure like creating a wrapper for `Set-Content` /// I'll post an answer later if nobody does, bit busy right now – Santiago Squarzon Oct 14 '22 at 14:55
  • Thanks very much, looking forward to that. I've been banging my head against this for a few hours, convinced it was something I was doing wrong, I didn't think that `Tee-Object` would just be broken. If it's possible, ideally, what I want is something that can go on a single line, i.e. `"my string is here" | some magic | some magic | Add-Content `. I'll probably use such a construct quite a lot if it fixes the problem of "just output to screen and to text file". – YorSubs Oct 14 '22 at 15:03
  • Oh, I was also heavily warned off using `Out-File` to ever write information to a file (apart from initial file creation) here, so I don't like to ever use `Out-File` as a means to append to a file. I see in that other answer I referenced above that they suggest using `Out-File` as a workaround but that looks like a bad way to go as indicated by my question in the following link. https://stackoverflow.com/questions/73630559/powershell-cannot-remove-bom-from-file – YorSubs Oct 14 '22 at 15:29

1 Answers1

1

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
js2010
  • 23,033
  • 6
  • 64
  • 66
  • Ah, I see you edited in a solution... I have never seen `-PassThru` before, and it seems to work perfectly, thanks. I note your rule on `out-file -append`, `tee-object -append` and `>>`, that sounds a good rule and I will never use those again. The way Microsoft have set these things up is so strange; I really do think PowerShell is an amazing language, but these odd cmdlet behaviours don't help anyone (and have probably cost people countless thousands of hours in confusion). Thanks, I will stick with `add-content -passthru` from now on. – YorSubs Oct 14 '22 at 17:34