Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Setting Encoding for Get-Content Pipeline

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?

like image 428
jedatu Avatar asked Sep 16 '08 20:09

jedatu


People also ask

How do I set encoding in PowerShell?

PowerShell uses a Unicode character set by default. However, several cmdlets have an Encoding parameter that can specify encoding for a different character set. This parameter allows you to choose the specific the character encoding you need for interoperability with other systems and applications.

How do I use the Get-Content command in PowerShell?

The Get-Content cmdlet gets the content of the item at the location specified by the path, such as the text in a file or the content of a function. For files, the content is read one line at a time and returns a collection of objects, each of which represents a line of content.

How do I get the content of a text file in PowerShell?

When you want to read the entire contents of a text file, the easiest way is to use the built-in Get-Content function. When you execute this command, the contents of this file will be displayed in your command prompt or the PowerShell ISE screen, depending on where you execute it.


2 Answers

As suggested here:

Get-Content tmp.log | Out-File -Encoding UTF8 new.log 
like image 87
Lars Truijens Avatar answered Oct 04 '22 02:10

Lars Truijens


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.

like image 42
driis Avatar answered Oct 04 '22 03:10

driis