title: PowerShell: how to convert a COM object to an .NET interop type?
body: <p>As described in my question <a href="https://stackoverflow.com/questions/8324075">Create ISO image using PowerShell: how to save IStream to file?</a>, in PowerShell I create an <a href="http://technet.microsoft.com/en-us/query/aa380034" rel="nofollow noreferrer"><code>IStream</code></a> object as follows:</p>
  
  <pre><code>$is = (New-Object -ComObject IMAPI2FS.MsftFileSystemImage).CreateResultImage().ImageStream
  </code></pre>
  
  <p>This object is of (PowerShell) type <code>System.__ComObject</code>.  And somehow PowerShell knows that it is an <code>IStream</code>:</p>
  
  <pre><code>PS C:\&gt; $is -is [System.Runtime.InteropServices.ComTypes.IConnectionPoint]
  False
  PS C:\&gt; $is -is [System.Runtime.InteropServices.ComTypes.IStream]
  True
  </code></pre>
  
  <p>However, a cast to this type fails:</p>
  
  <pre><code>PS C:\&gt; [System.Runtime.InteropServices.ComTypes.IStream] $is
  Cannot convert the "System.__ComObject" value of type "System.__ComObject" to type "System.Runtime.InteropServices.ComT
  ypes.IStream".
  At line:1 char:54
  + [System.Runtime.InteropServices.ComTypes.IStream] $is &lt;&lt;&lt;&lt;
      + CategoryInfo          : NotSpecified: (:) [], RuntimeException
      + FullyQualifiedErrorId : RuntimeException
  </code></pre>
  
  <p>How do I make this conversion work, without using C# code?</p>
  
  <p><strong>Update:</strong> Apparently this conversion cannot be made to work, as x0n's answer says.</p>
  
  <p>Now, my goal is to pass this <code>IStream</code> COM object to some C# code (part of the same PowerShell script using <code>Add-Type</code>), where it would become a .NET object of type <code>System.Runtime.InteropServices.ComTypes.IStream</code>.  Is that possible?  If not, what alternatives do I have?</p>