I'm currently working on some search and replace operation that I'm trying to automate using powershell. Unfortunately I recognized yesterday that we've different file encodings in our codebase (UTF8 and ASCII). Because we're doing these search and replace operations in a different branch I can't change the file encodings at this stage.
If I'm running the following lines it changes all files to UCS-2 Little Eindian even though my default powershell encoding is set to iso-8859-1 (Western European (Windows)).
$content = Get-Content $_.Path
$content -replace 'myOldText' , 'myNewText' | Out-File $_.Path
Is there a way to prevent powershell from changing the file's encoding?
Out-File
has a default encoding unless overriden with the-Encoding
parameter:What I've done to solve this is to try to get the original file's encoding by reading trying to read it's byte order mark and using it as the
-Encoding
parameter value.Here's an example processing a bunch of text file paths, getting the original encoding, processing the content and writing it back to file with the original's encoding.
Update Here is an example of getting the original file encoding using the StreamReader class. The example reads the first 3 bytes of the file so that the
CurrentEncoding
property gets set based on the result of its internal BOM detection routine.http://msdn.microsoft.com/en-us/library/9y86s1a9.aspx
http://msdn.microsoft.com/en-us/library/system.text.encoding.getpreamble.aspx