24/7/365 Support

Create an Instance of a .NET Object

Problem

You want to create an instance of a .NET object to interact with its methods and properties.

Solution

Use the NewObject cmdlet to create an instance of an object.

To create an instance of an object using its default constructor, use the NewObject cmdlet with the class name as its only parameter:

PS >$generator = NewObject System.Random PS >$generator.NextDouble() 0.853699042859347

To create an instance of an object that takes parameters for its constructor, supply those parameters to the NewObject cmdlet. In some instances, the class may exist in a separate library not loaded in PowerShell by default, such as the System.Windows. Forms assembly. In that case, you must first load the assembly that contains the class:

[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $image = NewObject System.Drawing.Bitmap source.gif $image.Save("source_converted.jpg","JPEG")

To create an object and use it at the same time (without saving it for later), wrap the call to NewObject in parentheses:

PS >(NewObject Net.WebClient).DownloadString("http://live.com")

Discussion

Many cmdlets (such as GetProcess and GetChildItem) generate live .NET objects that represent tangible processes, files, and directories. However, PowerShell supports much more of the .NET Framework than just the objects that its cmdlets produce.

These additional areas of the .NET Framework supply a huge amount of functionality that you can use in your scripts and general system administration tasks.

When it comes to using most of these classes, the first step is often to create an instance of the class, store that instance in a variable, and then work with the methods and properties on that instance. To create an instance of a class, you use the NewObject cmdlet. The first parameter to the NewObject cmdlet is the type name, and the second parameter is the list of arguments to the constructor, if it takes any. The NewObject cmdlet supports PowerShell’s type shortcuts, so you never have to use the fully qualified type name.

Since the second parameter to the NewObject cmdlet is an array of parameters to the type’s constructor, you might encounter difficulty when trying to specify a parameter that itself is a list. Assuming $byte is an array of bytes:

PS >$memoryStream = NewObject System.IO.MemoryStream $bytes NewObject : Cannot find an overload for ".ctor" and the argument count: "11". At line:1 char:27

+ $memoryStream = NewObject <<<< System.IO.MemoryStream $bytes

To solve this, provide an array that contains an array:

PS >$parameters = ,$bytes PS >$memoryStream = NewObject System.IO.MemoryStream $parameters

or

PS >$memoryStream = NewObject System.IO.MemoryStream @(,$bytes)

Load types from another assembly

PowerShell makes most common types available by default. However, many are available only after you load the library (called the assembly) that defines them. The MSDN documentation for a class includes the assembly that defines it.

To load an assembly, use the methods provided by the System.Reflection.Assembly class:

PS >[Reflection.Assembly]::LoadWithPartialName("System.Web")

GAC
Version
Location

True

v2.0.50727
C:\WINDOWS\assembly\GAC_32\(…)\System.Web.dll

PS >[Web.HttpUtility]::UrlEncode("http://search.msn.com") http%3a%2f%2fsearch.msn.com

The LoadWithPartialName method is unsuitable for scripts that you want to share with others or use in a production environment. It loads the most current version of the assembly, which may not be the same

as the version you used to develop your script. To load an assembly in the safest way possible, use its fully qualified name with the [Reflection.Assembly]::Load() method.

For more information about the NewObject cmdlet, type GetHelp NewObject.

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say