Create an Instance of a .NET Object
1. Problem
You want to create an instance of a .NET object to interact with its methods and properties.
2. Solution
Use the New-Object cmdlet to create an instance of an object.
To create an instance of an object using its default constructor, use the New-Object cmdlet with the class name as its only parameter:
PS >$generator = New-Object 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 New-Object 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 = New-Object 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 New-Object in parentheses:
PS >(New-Object Net.WebClient).DownloadString("http://live.com")
3. Discussion
Many cmdlets (such as Get-Process and Get-ChildItem) 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 New-Object cmdlet. The first parameter to the New-Object cmdlet is the type name, and the second parameter is the list of arguments to the constructor, if it takes any. The New-Object cmdlet supports PowerShell’s type shortcuts, so you never have to use the fully qualified type name. For more information about type shortcuts, see Section 3.4, “Work with .NET Objects.”
Since the second parameter to the New-Object 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:
Code View: Scroll / Show All
PS >$memoryStream = New-Object System.IO.MemoryStream $bytes
New-Object : Cannot find an overload for ".ctor" and the argument count: "11".
At line:1 char:27
+ $memoryStream = New-Object <<<< System.IO.MemoryStream $bytes
To solve this, provide an array that contains an array:
PS >$parameters = ,$bytes
PS >$memoryStream = New-Object System.IO.MemoryStream $parameters
or
PS >$memoryStream = New-Object System.IO.MemoryStream @(,$bytes)
3.1. 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

