Skip to content

Instantly share code, notes, and snippets.

@bjorn-ali-goransson
Created August 23, 2019 09:12
Show Gist options
  • Save bjorn-ali-goransson/2559b5b5fe674759004d7c55879059eb to your computer and use it in GitHub Desktop.
Save bjorn-ali-goransson/2559b5b5fe674759004d7c55879059eb to your computer and use it in GitHub Desktop.
Powershell script to remove UTF-8 BOM from files in current directory
# http://robertwesterlund.net/post/2014/12/27/removing-utf8-bom-using-powershell
foreach($file in Get-ChildItem -Path $PSScriptRoot -Include *.php -Recurse -ErrorAction Continue -Force) {
$reader = $file.OpenRead()
$byteBuffer = New-Object System.Byte[] 3
$bytesRead = $reader.Read($byteBuffer, 0, 3)
if ($bytesRead -eq 3 -and
$byteBuffer[0] -eq 239 -and
$byteBuffer[1] -eq 187 -and
$byteBuffer[2] -eq 191)
{
echo "Removing UTF8 BOM on $($file.FullName)"
$tempFile = [System.IO.Path]::GetTempFileName()
$writer = [System.IO.File]::OpenWrite($tempFile)
$reader.CopyTo($writer)
$writer.Dispose()
$reader.Dispose()
Move-Item -Path $tempFile -Destination $file.FullName -Force
}
else
{
$reader.Dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment