Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, August 3, 2010 7:08 PM
I have written one of my first PowerShell scripts to execute after I mirror my music collection to back it up. The PS script I wrote will replace a string of text using the "-replace" command. However, when the outfile is written, the original encoding is lost. For instance, the following line is the source:
<media src="\server\music\Eisbrecher\Antikörper\02 Adrenalin.mp3" />
And the output:
<media src="S:\Eisbrecher\Antikörper\02 Adrenalin.mp3" />
If I open the original file with Notepad++, I see the format is in "UTF-8 without BOM." I modified my script from:
$fileEntries = [IO.Directory]::GetFiles("\\Server\backup$\test\Playlists");
foreach($fileName in $fileEntries)
{
[Console]::WriteLine($fileName);
$c = Get-Content $fileName;
$n = $c -replace('\\\\server\\music','S:');
Out-File $fileName -inputobject $n;
}
to:
$fileEntries = [IO.Directory]::GetFiles("\\Server\backup$\test\Playlists");
foreach($fileName in $fileEntries)
{
[Console]::WriteLine($fileName);
$c = Get-Content $fileName;
$n = $c -replace('\\\\server\\music','S:');
Out-File $fileName -inputobject $n -encoding UTF8;
}
Is this a problem with Out-File and it's lack of proper encoding enforcement? Is there anyway to fix this, other than to rename my original files to ANSI format?
All replies (7)
Tuesday, August 3, 2010 7:27 PM | 1 vote
help Out-File -full has this description for -Encoding
-Encoding <string>
Specifies the type of character encoding
used in the file. Valid values are
"Unicode", "UTF7", "UTF8", "UTF32", "ASCII",
"BigEndianUnicode", "Default", and "OEM".
"Unicode" is the default.
These are string values.
So, wrap your value in quotes as "UTF8"
Tuesday, August 3, 2010 7:45 PM
Thanks for the reply. I did as you suggested, but same result.
Tuesday, August 3, 2010 8:18 PM
Look at
I think it should work with -encoding "UTF8"
- Larry
On 8/3/2010 2:45 PM, ChrisLynch wrote:
> Thanks for the reply. I did as you suggested, but same result.
Tuesday, August 3, 2010 8:30 PM
Understood. I believe that it should work as well, but it does not. I wonder if during the -replace process text is being converted to ANSI format, and then UTF-8 when writing the file. I am simply assuming here.
Monday, May 28, 2018 2:41 PM
Hi,
Do you remember how you managed your problem ? I get the same
regards
Monday, May 28, 2018 2:45 PM
Please do not reactivate ancient threads from other people. It is much likely that the original poster will not come back and answer you.
If neede create a new question for your problem. Before you should carefully review the following help topics:
Please read this first: This forum is for scripting questions rather than script requests.
Also find scripts here: PowerShell Gallery or here: TechNet Gallery - resources for IT professionals.
Learn PowerShell: Microsoft Virtual Academy - Getting Started with Microsoft PowerShell.
Script requests: Microsoft Technet Script Center - Requests.
Best regards,
(79,108,97,102|%{[char]$_})-join''
Thursday, July 26, 2018 8:06 PM
Hi mi jo,
the problem in the above code is that the encoding happens at two separate instances, both when the content goes in and out.
You also have to change the encoding for Get-Content accordingly, otherwise you'll end up having the garbled characters from the very beginning in your script.
Get-Content -Encoding UTF8
Out-File -Encoding UTF8
Regards,
Dequester