Powershell Get-Content -> Foreach-Object -> -replace ->Out-File is adding a char (0x00) to the start of every file

Question:

I have a function that performs a regex replace in a file. The problem is that that it adds a character (0x00) to the start of every file it touches (even the ones that it doesn’t find a match for!). Since I am editing csproj files, MSBuild gives me this error:

Here is my function:

How can I replace the lines I want and not change any other part of the file?

Answer:

It sounds like it’s writing a BOM at the beginning of the file. You can set the encoding to ASCII (which has no BOM) using the -Encoding ASCII parameter on out-file.

Source:

Powershell Get-Content -> Foreach-Object -> -replace ->Out-File is adding a char (0x00) to the start of every file by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply