13.3. Using Synthetic Types

The .NET type system that Windows PowerShell uses is extensible. This extensibility is directly relevant to how Windows PowerShell handles COM objects.

All COM objects have the same type, System._ComObject, in a .NET Framework setting. That causes potential problems for Windows PowerShell in distinguishing one COM object from another, since although all COM objects have the System._ComObject type, there is a huge variety of underlying functionality and a large range of members of individual object instances. If you can't unambiguously identify the kind of object you are dealing with, it makes writing code to use methods or access or manipulate properties highly problematic. To resolve that ambiguity, when creating a COM object Windows, PowerShell creates a synthetic type that uses the common System._ComObject type and adds a # to it followed by the Class ID stored in the registry for the relevant class.

The following example displays the synthetic type for Internet Explorer. Create a new instance of Internet Explorer, using the command:

$ie = new-object -ComObject InternetExplorer.application

Then use the following command to display the members of the $ie object:

$ie |
get-member

A lengthy list of members is displayed, but before those the synthetic type, which refers to the registry class for Internet Explorer, is displayed. Figure 13-14 shows the synthetic type for Internet Explorer. It is displayed as System._ComObject#{d30c1661-cdaf-11d0-8a3e-00c04fc9e26e} ...

Get Professional Windows® PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.