Skip to content

Instantly share code, notes, and snippets.

@gravejester
Created February 9, 2016 09:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravejester/3d8903291094f80eb4ca to your computer and use it in GitHub Desktop.
Save gravejester/3d8903291094f80eb4ca to your computer and use it in GitHub Desktop.
function Show-BinaryFile {
<#
.SYNOPSIS
Binary file viewer.
.DESCRIPTION
This function will read and output a HEX and ASCII representation of a
binary file, similar to hex editors.
.EXAMPLE
Show-BinaryFile -Path 'c:\path\to\binary.file'
This will read and output the entire binary file.
.EXAMPLE
Show-BinaryFile -Path 'c:\path\to\binary.file' -Start 16 -Length 64
This will read 64 bytes starting at position 16, and output the results.
.NOTES
NOTE! This function is using my wrapper function Invoke-CryptBinaryToString and will not work without it!
Author: Øyvind Kallstad
Date: 09.02.2016
Version: 1.0
.LINK
https://communary.wordpress.com/
#>
[CmdletBinding()]
param (
# The path to the file you want to read.
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true, ValueFromPipelinebyPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[string] $Path,
# Start position for read operation.
[Parameter(Position = 1)]
[ValidateRange(0,[int]::MaxValue)]
[int] $Start = 0,
# The length of the read operation.
[Parameter(Position = 2)]
[ValidateRange(0,[int]::MaxValue)]
[int] $Length = 0,
# Indicate whether you want an address portion added to the output.
[Parameter(Position = 3)]
[switch] $ShowAddress
)
if (Test-Path -Path $Path) {
try {
$resolvedPath = Resolve-Path -Path $Path
$fileStream = New-Object -TypeName System.IO.FileStream -ArgumentList ($resolvedPath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
$fileReader = New-Object -TypeName System.IO.BinaryReader -ArgumentList $fileStream
if ($Length -eq 0) {
$Length = $fileReader.BaseStream.Length
}
$fileReader.BaseStream.Position = $Start
[byte[]]$readBytes = $fileReader.ReadBytes($Length)
if ($ShowAddress) {
$readBytes | Invoke-CryptBinaryToString -Format HexASCIIAddr
}
else {
$readBytes | Invoke-CryptBinaryToString -Format HexASCII
}
}
catch {
Write-Warning $_.Exception.Message
}
finally {
$fileReader.Dispose()
$fileStream.Dispose()
}
}
else { Write-Warning "$Path not found!" }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment