Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding problems when concatenating with Powershell

Out of a bunch of files I want to concatenate all files whose filename starts with a 1 into a textfile named 1.txt. The encoding of the source files is UTF16-LE and this shall also be the encoding of the target file. But using my powershell script (code below) results in a UTF-8 file with defect special signs (Umlauts etc.)

This is my code:

Powershell.exe cat  Disc:\data\files\1\filename-1* > Path:\here\1.txt

I found several approaches using Get-Content and Out-File but was not able to adapt those commands to my wildcard needs. As far as I understand, cat is a standard alias of get-content, so I wonder why I cannot just replace cat with get-content. Additionally, get-content has a -Encoding CharSet parameter but changing my code like this:

Powershell.exe cat  Disc:\data\files\1\filename-1* > Path:\here\1.txt -encode Unicode

(which is the charset for UTF-16 format little-endian byte order) does not change a thing. Neither does BigEndianUnicode or UTF8.

Thanks for any help!

like image 931
Largo Avatar asked Sep 26 '26 17:09

Largo


1 Answers

You can't specify the encoding when using a redirection operator (>). Use the Out-File cmdlet instead:

Get-Content C:\path\to\input* | Out-File D:\path\to\output.txt -Encoding Unicode
like image 187
Ansgar Wiechers Avatar answered Sep 28 '26 10:09

Ansgar Wiechers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!