Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Remove-UTF8BOMEncoding {
- [CmdletBinding()]
- Param (
- [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)][Alias('PSPath')][string] $Path
- )
- begin {
- $utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
- }
- process {
- foreach ($p in $Path) {
- Try {
- [System.IO.FileInfo] $file = Get-Item -Path $p
- $sequenceBOM = New-Object System.Byte[] 3
- $reader = $file.OpenRead()
- $bytesRead = $reader.Read($sequenceBOM, 0, 3)
- $reader.Dispose()
- #A UTF-8+BOM string will start with the three following bytes. Hex: 0xEF0xBB0xBF, Decimal: 239 187 191
- if ($bytesRead -eq 3 -and $sequenceBOM[0] -eq 239 -and $sequenceBOM[1] -eq 187 -and $sequenceBOM[2] -eq 191) {
- [System.IO.File]::WriteAllLines($file.FullName, (Get-Content $file.FullName), $utf8NoBomEncoding)
- Write-Verbose "Remove UTF-8 BOM successfully from $($file.PSChildName)"
- }
- } Catch [Exception] {
- Write-Error $_.Exception.ToString()
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement