62 lines
1.3 KiB
PowerShell
62 lines
1.3 KiB
PowerShell
|
function Test-FileBOM {
|
||
|
<#
|
||
|
.SYNOPSIS
|
||
|
Tests given file if native utf8 w/o BOM is used. Returns false if BOM is present.
|
||
|
|
||
|
.DESCRIPTION
|
||
|
This function is used to test for a valid encoding without BOM.
|
||
|
|
||
|
.PARAMETER Path
|
||
|
Full or relative path to existing file.
|
||
|
|
||
|
.INPUTS
|
||
|
[None]
|
||
|
|
||
|
.OUTPUTS
|
||
|
[bool]
|
||
|
|
||
|
.EXAMPLE
|
||
|
Test-FileBOM -Path './Testfile.txt'
|
||
|
|
||
|
.NOTES
|
||
|
#>
|
||
|
|
||
|
[CmdletBinding()]
|
||
|
[OutputType([bool])]
|
||
|
|
||
|
param (
|
||
|
[Parameter(Mandatory = $true)]
|
||
|
[ValidateScript(
|
||
|
{
|
||
|
Test-Path -Path $_
|
||
|
}
|
||
|
)]
|
||
|
[string]$Path
|
||
|
)
|
||
|
|
||
|
begin {
|
||
|
}
|
||
|
|
||
|
process {
|
||
|
try {
|
||
|
$contents = [byte[]]::new(3)
|
||
|
$stream = [System.IO.File]::OpenRead($Path)
|
||
|
$stream.Read($contents, 0, 3) | Out-Null
|
||
|
$stream.Close()
|
||
|
}
|
||
|
catch {
|
||
|
Write-Error -Message 'Could not read the given file!' -ErrorAction 'Stop'
|
||
|
}
|
||
|
Write-Debug -Message ('BOM Content was: {0}' -f ([System.BitConverter]::ToString($contents)))
|
||
|
if ( $contents[0] -eq 0xEF -and $contents[1] -eq 0xBB -and $contents -eq 0xBF ) {
|
||
|
Write-Output $false
|
||
|
}
|
||
|
else {
|
||
|
Write-Output $true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
end {
|
||
|
}
|
||
|
}
|