Discussion PowerShellscript, bad file encoding

I have a PowerShell script for talking character encoding file.

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 the character encoding of the file to utf-16. When I call the iconv utility from the command line, it works fine.

How am I wrong?

+4
source share
1 answer

When redirecting output to a file, Powershell uses Unicode as the default encoding. Instead of using the redirection operator, you can connect to the Out-File using the -Encoding UTF8 switch.

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

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

In case this helps your script at all, it's worth noting that you can also use Powershell to convert the encoding.

 Get-Content $inFileName -Encoding ASCII | Out-File -FilePath $outFileName -Encoding UTF8 
+6
source

All Articles