title: Output binary data on PowerShell pipeline
body: <p>I need to pipe some data to a program's <code>stdin</code>:</p>
  <ol>
  <li>First 4 bytes are a 32-bit <code>unsigned int</code> representing the length of the data.  These 4 bytes are exactly the same as C would store an <code>unsigned int</code> in memory.  I refer to this as binary data.</li>
  <li>Remaining bytes are the data.</li>
  </ol>
  <p>In C, this is trivial:</p>
  <pre class="lang-c prettyprint-override"><code>WriteFile(h, &amp;cb, 4);  // cb is a 4 byte integer
  WriteFile(h, pData, cb);
  </code></pre>
  <p>or</p>
  <pre class="lang-c prettyprint-override"><code>fwrite(&amp;cb, sizeof(cb), 1, pFile);
  fwrite(pData, cb, 1, pFile);
  </code></pre>
  <p>or in C# you would use a <code>BinaryWriter</code> (I think this code is right, i don't have C# lying around right now...)</p>
  <pre class="lang-cs prettyprint-override"><code>Bw.Write((int)Data.Length);
  Bw.Write(Data, 0, Data.Length);
  </code></pre>
  <p>In PowerShell I'm sure it's possible, but this is as close as I could get.  This is obviously printing out the 4 bytes of the size as 4 human readable numbers:</p>
  <pre><code>$file = &quot;c:\test.txt&quot;
  Set-content $file &quot;test data&quot; -encoding ascii
  [int]$size = (Get-ChildItem $file).Length
  $bytes = [System.BitConverter]::GetBytes($size)
  $data = Get-content $file
  $bytes
  $data
  </code></pre>
  <pre class="lang-none prettyprint-override"><code>11
  0
  0
  0
  test data
  </code></pre>
  <p>I need the binary data sent out on the pipe to look like this (<code>\xA</code> is the escaped representation of a non printable character, I don't want <code>'\'</code> in my output, I want the BYTE that <code>'\xA'</code> represents in the output) :</p>
  <pre class="lang-none prettyprint-override"><code>\xA\x0\x0\0test data
  </code></pre>
  <p>I don't know how to write a byte array out the pipeline in binary format.  I also don't know how to get rid of the carriage returns.</p>
  <p><strong>EDIT:</strong>
  I have found that I can do this:</p>
  <pre><code>$file = &quot;c:\test.txt&quot;
  Set-content $file &quot;test data&quot; -encoding ascii
  &quot;File: &quot;&quot;{0}&quot;&quot;&quot; -f (Get-content $file)
  [int]$size = (Get-ChildItem $file).Length
  &quot;Size: &quot; + $size
  $bytes = [System.BitConverter]::GetBytes($size)
  &quot;Bytes: &quot; + $bytes
  $data = Get-content $file
  $file1 = &quot;c:\test1.txt&quot;
  Set-content $file1 $bytes -encoding byte
  Add-Content $file1 $data -encoding ASCII
  &quot;File: &quot;&quot;{0}&quot;&quot;&quot; -f (Get-content $file1)
  &quot;Size: &quot; + (Get-ChildItem $file1).Length
  </code></pre>
  <pre class="lang-none prettyprint-override"><code>File: &quot;test data&quot;
  Size: 11
  Bytes: 11 0 0 0
  File: &quot;   test data&quot;
  Size: 15
  </code></pre>
  <p>But this requires me to build a temporary file.  There must be a better way!</p>
  <p><strong>EDIT:</strong>
  That solution above, corrupts any character code &gt; <code>127</code>.  There is no &quot;binary&quot; encoding mode for the pipe.</p>
  <p><strong>EDIT:</strong>
  I finally discovered a roundabout way to get a <code>BinaryWriter</code> wired up to an application's <code>stdin</code>.  See <a href="https://stackoverflow.com/a/24745250/">my answer</a>.</p>