There has never been an easier way to create a new instance of a .NET framework component or COM object. It is possible to do so by using the cmdlet “New-Object” in PowerShell. Doing so enables one to control and startup the other applications via PowerShell. Moreover, it also allows the creation of custom objects having their own set of data item collections.
In this guided post, you will find about the “New-Object” cmdlet.
What is New-Object (Microsoft.PowerShell.Utility)?
As stated in the above section, the “New-Object” cmdlet creates new instances of the .NET framework. Let’s overview the following examples to get a better understanding.
Example 1: Make a “System.Version” Object Using the “New-Object” Cmdlet
To create a “System.Version” object, execute the below-provided command:
According to the above-given code:
- First, write the “New-Object” cmdlet alongside the “-TypeName” parameter with the value “Version” assigned to it.
- Next, add the “-ArgumentList” parameter and assign the stated value to it:
Example 2: Use the “New-Object” Cmdlet to Create an Internet Explorer COM object
Run the below-mentioned command to initiate an Internet Explorer object:
In the above-stated code:
- First, initialize a variable “$New” and assign the stated code.
- To begin with, first, assign the “New-Object” cmdlet alongside the “-COMObject” parameter having the stated value assigned to it.
- Next, write down the “-Property” parameter and assigned the above-mentioned value:
Example 3: Use the “New-Object” Cmdlet Along With the “-String” Parameter to Create a Non-Terminating Error
To initiate a non-terminating error in PowerShell, use the given command:
According to the above code:
- First, initialize a variable and assign it the stated code.
- Next, specify the “New-Object” cmdlet, along with the “-COMObject” parameter having the stated value assigned to it.
- After that, mention the parameters “-Strict”, and “-Property”.
- Lastly, provide a specified value:
That was all about the “New-Object” cmdlet in PowerShell.
Conclusion
The “New-Object” in PowerShell instantiates a new object of a COM and the .NET framework. Moreover, it can help create custom objects having their own set of data items. This article explored the various ways to create new objects using the “New-Object” cmdlet.