4

I have a PowerShell script for the conversation of file character encoding.

Get-ChildItem -Path D:/test/data -Recurse -Include *.txt |
ForEach-Object {
  $inFileName = $_.DirectoryName + '\' + $_.name
  $outFileName = $inFileName + "_utf_8.txt"
  Write-Host "windows-1251 to utf-8: " $inFileName -> $outFileName  
  E:\bin\iconv\iconv.exe -f cp1251 -t utf-8 $inFileName > $outFileName
}

But instead of utf-8 it converts file character encoding into utf-16. When I invoke the iconv utility from command line it works fine.

What do I wrong?

Agent Coop
  • 392
  • 4
  • 12

1 Answers1

5

When you redirect output to a file, Powershell is using Unicode as the default encoding. Instead of using the redirection operator, you can pipe to Out-File with a -Encoding UTF8 switch.

E:\bin\iconv\iconv.exe -f cp1251 -t utf-8 $inFileName | Out-File -FilePath $outFileName -Encoding UTF8

The following TechNet article has more information (equivalent to Get-Help Out-File -full in Powershell v2).

In case it helps your scenario at all, it's worth noting that you can use Powershell to do the encoding conversion also.

Get-Content $inFileName -Encoding ASCII |
Out-File -FilePath $outFileName -Encoding UTF8
ajk
  • 4,473
  • 2
  • 19
  • 24
  • Are you sure there is such a parameter for the Get-Content cmdlet as -Encoding? – Agent Coop Sep 28 '11 at 17:02
  • @Gustav.Calder - Yeah there is – manojlds Sep 28 '11 at 17:17
  • 2
    @Gustav.Calder There is, but it doesn't appear in the help for `Get-Content` because it's a dynamic parameter provided by the FileSystem provider. You can find more information in `Get-Help FileSystem`. – ajk Sep 28 '11 at 17:52