47

I have a file saved as UCS-2 Little Endian I want to change the encoding so I ran the following code:

cat tmp.log -encoding UTF8 > new.log

The resulting file is still in UCS-2 Little Endian. Is this because the pipeline is always in that format? Is there an easy way to pipe this to a new file as UTF8?

jedatu
  • 3,893
  • 6
  • 43
  • 59

5 Answers5

56

As suggested here:

Get-Content tmp.log | Out-File -Encoding UTF8 new.log
Community
  • 1
  • 1
Lars Truijens
  • 40,852
  • 6
  • 117
  • 137
31

I would do it like this:

get-content tmp.log -encoding Unicode | set-content new.log -encoding UTF8

My understanding is that the -encoding option selects the encdoing that the file should be read or written in.

driis
  • 151,614
  • 43
  • 262
  • 332
8

load content from xml file with encoding.

(Get-Content -Encoding UTF8 $fileName)

user572730
  • 108
  • 1
  • 2
  • Do not use `Get-Content` to read XML files, because this requires prior knowledge of the file encoding. It's much better to let the XML parser read the file on it's own, as this correctly deals with any encoding the file might have. `$xml = (New-Object System.Xml.XmlDocument).Load($fileName)` – Tomalak Sep 25 '17 at 09:45
1

If you are reading an XML file, here's an even better way that adapts to the encoding of your XML file:

$xml = New-Object -Typename XML
$xml.load('foo.xml')
sba923
  • 459
  • 5
  • 8
0

PowerShell's get-content/set-content encoding flag doesn't handle all encoding types. You may need to use IO.File, for example to load a file using Windows-1252:

$myString = [IO.File]::ReadAllText($filePath, [Text.Encoding]::GetEncoding(1252))

Text.Encoding::GetEncoding Text.Encoding::GetEncodings

Geordie
  • 1,132
  • 14
  • 24