Powershell Format-Hex how to truncate decoded text column on right

Viewed 90

Hi I'm trying to use power shell command Format-Hex and it works well . In normal hex editor there is option to hide and not include the decoded text characters on the right .

I couldnt figure out how to use Format-Hex in power shell to only show :

 00000030   1A 00 00 00 00 00 00 00 68 74 74 70 3A 2F 2F 66 

Is there a parameter to do that with PS C:\Users\YourName\Desktop>Format-Hex file.dll instead of the below ? I am just confirming if any such parameter or cli tool that exists to allow when using Format-Hex command not looking for truncation or file parsing solutions .

00000030   1A 00 00 00 00 00 00 00 68 74 74 70 3A 2F 2F 66  ........http://f
00000040   72 61 6E 6B 69 65 2E 70 68 6F 74 6F 00 00 FF ED  rankie.photo...í
00000050   00 C2 50 68 6F 74 6F 73 68 6F 70 20 33 2E 30 00  .APhotoshop 3.0.
00000060   38 42 49 4D 04 04 00 00 00 00 00 A6 1C 02 28 00  8BIM.......▌..(.
00000070   62 46 42 4D 44 30 31 30 30 30 61 66 63 30 64 30  bFBMD01000afc0d0

I read the help on Format-Hex

1 Answers

Format-Hex has no parameter that would omit the last column from its output.

If you're running on macOS or Linux, you can use the standard utilities available there, such as hexdump.

However, at the expense of performance, you can write a simple wrapper function around Format-Hex:

function Format-HexBare {
  Format-Hex @args | 
    Out-String -Stream | 
      ForEach-Object `
        -Begin { $maxLen = if ($IsCoreCLR) { 64 } else { 58 } } `
        -Process {
          $line = $_
          try { $line.Substring(0, $maxLen) } catch { $line }
        }
}

Note: The - PowerShell edition-specific - max. line length excluding the last column is hard-coded above, for the following reasons:

  • A robust regex solution would be challenging.

  • The .Substring() solution also eliminates the header of the unwanted column (which is only displayed in PowerShell (Core) 7+).

While there is no guarantee that output formats won't change over time,[1] it is unlikely to in this case. Should it happen nonetheless, the function is easy to adapt.


[1] The specifics of the for-display formatting - meant for the human observer only - aren't part of PowerShell's public contract with respect to backward compatibility.

Related