Skip to content

Instantly share code, notes, and snippets.

@brianvanderlugt
Created October 27, 2017 10:43
Show Gist options
  • Save brianvanderlugt/5f517935be9b2b467c3aa64fcde3c98d to your computer and use it in GitHub Desktop.
Save brianvanderlugt/5f517935be9b2b467c3aa64fcde3c98d to your computer and use it in GitHub Desktop.
BOM Removal with PowerShell
function Remove-BOMFromFile{
param(
[Parameter(Mandatory)]
[ValidateScript({Test-Path -Path $_ -PathType Container})]
[String]$Path
)
foreach ($file in (Get-ChildItem -Path $Path -Include '*.hql' -Recurse))
{
if(Test-BOMInFile -File $file){
$tmpFile = [System.IO.Path]::GetTempFileName()
Get-Content $file.FullName -Raw | %{$_ -replace "\xEF\xBB\xBF",''} | Set-Content $tmpFile
Move-Item -Path $tmpFile -Destination $file.FullName -Force
}
}
}
function Test-BOMInFile{
param(
[System.IO.FileSystemInfo]$File
)
[byte[]]$bytes = Get-Content -Encoding Byte -ReadCount 3 -TotalCount 3 -Path $File.FullName
if( $bytes[0] -eq 0xef -and $bytes[1] -eq 0xbb -and $bytes[2] -eq 0xbf )
{
return $true
}
else
{
$false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment