Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active December 22, 2018 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/3ee289271e9a1f1590fef36dd70c2efe to your computer and use it in GitHub Desktop.
Save guitarrapc/3ee289271e9a1f1590fef36dd70c2efe to your computer and use it in GitHub Desktop.
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