title: Creating Byte[] in PowerShell
body: <p>I have some PowerShell code that is using a COM API. I am getting a Type Mismatch error when I pass in a byte array. Here is how I am creating the array, as well as some type information</p>
  
  <pre><code>PS C:\&gt; $bytes = Get-Content $file -Encoding byte
  PS C:\&gt; $bytes.GetType()
  
  IsPublic IsSerial Name                                     BaseType
  -------- -------- ----                                     --------
  True     True     Object[]                                 System.Array
  
  
  PS C:\&gt; $bytes[0].GetType()
  
  IsPublic IsSerial Name                                     BaseType
  -------- -------- ----                                     --------
  True     True     Byte                                     System.ValueType
  </code></pre>
  
  <p>Poking around with the API, I have found that it is looking for a Byte[] with a base type of System.Array. </p>
  
  <pre><code>PS C:\&gt; $r.data.GetType()
  
  IsPublic IsSerial Name                                     BaseType
  -------- -------- ----                                     --------
  True     True     Byte[]                                   System.Array
  
  PS C:\&gt; $r.data[0].gettype()
  
  IsPublic IsSerial Name                                     BaseType
  -------- -------- ----                                     --------
  True     True     Byte                                     System.ValueType
  </code></pre>
  
  <p>What I am trying to do is convert $bytes into the same type as $r.data. For some reason, $bytes is getting created as an Object[]. How can I cast  it to a Byte[]?</p>