Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function Test-Utf8BomHeaderPS {
[CmdletBinding(DefaultParameterSetName = "File")]
[OutputType([bool])]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "File")]
[System.IO.FileInfo]$File,
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "Path")]
[Alias("LiteralPath", "PSPath")]
[string]$Path
)
process {
if ($PSBoundParameters.ContainsKey("File")) {
[System.IO.FileStream]$stream
try {
$stream = $File.OpenRead()
$header = (1..3 | ForEach-Object {$stream.ReadByte().ToString("X2")}) -join ""
Write-Output ($header -eq $bomHex)
}
finally {
$stream.Dispose();
}
}
else {
[System.IO.FileStream]$stream
try {
$stream = [System.IO.FileStream]::New($Path, [System.IO.FileMode]::Open)
$header = (1..3 | ForEach-Object {$stream.ReadByte().ToString("X2")}) -join ""
Write-Output ($header -eq $bomHex)
}
finally {
$stream.Dispose();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.