14.2. Creating .NET Objects

The new-object cmdlet helps you create .NET objects and COM objects. It is described in the next section. However, although you will probably use it frequently to create a new .NET object, the new-object cmdlet is not the only way to create a new .NET object.

You can also create an object by casting a string to the desired object type, assuming that the cast is a valid one. I describe that technique in the section following the new-object cmdlet section.

14.2.1. The new-object Cmdlet

The new-object cmdlet is used to create new .NET objects and new COM objects. Using the new-object cmdlet to create COM objects is described in Chapter 13. In this section, I focus on using the new-object cmdlet to create new .NET objects.

The new-object cmdlet supports the following parameters (in addition to the common parameters, which are described in Chapter 6):

  • TypeName — The fully qualified name of the .NET type to be created. It is both a required parameter and a positional parameter.

  • ArgumentList — Arguments to the constructor of the type specified in the Arguments parameter. An optional parameter.

  • ComObject — Used only when creating COM objects.

  • Strict — Used only when creating COM objects.

The -ComObject and -Strict parameters relate specifically to the use of the new-object cmdlet with COM objects and is covered in Chapter 13.

To create a new .NET object, you simply provide a valid .NET type name as the value of the TypeName parameter to the new-object cmdlet. ...

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.