Created
October 27, 2017 10:43
-
-
Save brianvanderlugt/5f517935be9b2b467c3aa64fcde3c98d to your computer and use it in GitHub Desktop.
BOM Removal with PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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