forked from gangstanthony/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-Header.ps1
26 lines (22 loc) · 826 Bytes
/
Get-Header.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# http://learningpcs.blogspot.com/2012/07/powershell-v3-check-file-headers.html
# https://en.wikipedia.org/wiki/List_of_file_signatures
# http://www.garykessler.net/library/file_sigs.html
function Get-Header {
param (
[string]$path,
[int]$bits = 4
)
$path = Resolve-FullPath $path
try {
# Get content of each file (up to 4 bytes) for analysis
$HeaderAsHexString = New-Object System.Text.StringBuilder
[Byte[]](Get-Content -Path $path -TotalCount $bits -Encoding Byte -ea Stop) | % {
if (("{0:X}" -f $_).length -eq 1) {
$null = $HeaderAsHexString.Append('0{0:X}' -f $_)
} else {
$null = $HeaderAsHexString.Append('{0:X}' -f $_)
}
}
$HeaderAsHexString.ToString()
} catch {}
}