0

I've written a script to make lots of regular expression modifications to a file and overwrite the file with the changes. However I have just noticed all the files are being written with UTF-16LE encoding. I realise that I can specify the encoding for the Out-File cmdlet, but I'm not confident that all the input files are the same encoding. Can I get the encoding from Get-Content somehow, or should I be using some other method of I/O? An outline of the script is below

Get-ChildItem *.aspx -Recurse| ForEach-Object {
        MakeWritable($_)
        (Get-Content -raw $_) | FixUp($_.FullName)
}

Filter FixUp($filename)
{
    DoReplacements($_) | Out-File $filename
}
Rattle
  • 2,393
  • 2
  • 20
  • 25
  • 1
    Sounds like you'd need to validate the encoding of a particular file first. Does [this](https://stackoverflow.com/questions/3710374/get-encoding-of-a-file-in-windows) help at all? – gravity Aug 11 '17 at 15:32
  • 1
    Specifically this answer: https://stackoverflow.com/a/28079177/3829407 – Matt Aug 11 '17 at 15:34
  • Yes thanks, hopefully all the input files are the same and I can force encoding for both Get-Content and Out-File. – Rattle Aug 11 '17 at 15:37
  • They're not, but I can use that function to check the encoding before modifying the file. – Rattle Aug 11 '17 at 15:43
  • Possible duplicate of [Get encoding of a file in Windows](https://stackoverflow.com/questions/3710374/get-encoding-of-a-file-in-windows) – G42 Aug 11 '17 at 16:05

0 Answers0