title: Methods to hex edit binary files via Powershell
body: <p>Am trying to perform binary hex edit from the command line using only powershell.  Have had partial success performing a hex replace with this snip.  Problem springs up when 123456 occurs multiple times and the replacement was only supposed to occur at a specific location.</p>
  
  <p>NOTE: The snip requires the <code>Convert-ByteArrayToHexString</code> and <code>Convert-HexStringToByteArray</code> functions found here. </p>
  
  <p><a href="http://www.sans.org/windows-security/2010/02/11/powershell-byte-array-hex-convert" rel="noreferrer">http://www.sans.org/windows-security/2010/02/11/powershell-byte-array-hex-convert</a></p>
  
  <pre><code>$readin = [System.IO.File]::ReadAllBytes("C:\OldFile.exe");
  $hx = Convert-ByteArrayToHexString $readin -width 40 -delimiter "";
  $hx = $hx -replace "123456","FFFFFF";
  $hx = "0x" + $hx;
  $writeout = Convert-HexStringToByteArray $hx;
  set-content -value $writeout -encoding byte -path "C:\NewFile.exe";
  </code></pre>
  
  <p>How can we specify an offset position into powershell to replace this sketchy -replace command.</p>