Home > Software engineering >  PowerShell 5.1 v PWSH Out-File
PowerShell 5.1 v PWSH Out-File

Time:12-21

More of a curiosity question. Has anyone noticed a difference between PS(5.1) vs pwsh(7.2.1) and using the Out-File cmdlet?

Im talking more along the lines of you run a script in PS and it outputs a file test.log in this instance using out-file. And then you run the same script in pwsh and outputing to the same file with -Append and the text just shows as japanese(?) characters.

PowerShell 5 Test Text
PowerShell 5 Test Text
潐敷卲敨汬㜠吠獥⁴敔瑸਍潐敷卲敨汬㜠吠獥⁴敔瑸਍

The last line is what appears when running pwsh:

"PowerShell 7 Test Text" | Out-File "C:\Source\Scripts\ScriptLog\test.log" -Append

When doing the reverse pwsh to PowerShell the text appears, but it is formatted with spaces:

PowerShell 7 Test Text
P o w e r S h e l l   5   T e s t   T e x t 

Do the two programs just format text files differently? Or is there a way to have them format the same?

We are slowly working pwsh into being the norm but have to do quite a bit of regression testing to ensure things work properly and this is one thing I've been seeing that is not working.

CodePudding user response:

Well in doing some of my own digging/testing in the meantime. I found that if I specify -Encoding oem that seems to allow the outputs to match and be in a normal text format no matter what terminal I run from

"PowerShell 7 Test Text" | Out-File "C:\Source\Scripts\ScriptLog\test.log" -Append -Encoding oem

CodePudding user response:

Out-file -append and >> do not check the current file encoding, so they can mix utf16 and utf8 in the same file. I would recommend add-content instead.

  • Related