New-Object

 

SYNOPSIS

Creates an instance of a .Net or COM object.

 

SYNTAX

New-Object [-typeName] <string> [[-argumentList] <Object[]>] [<CommonParameters>]

 

New-Object [-comObject] <string> [-strict] [<CommonParameters>]

 

DETAILED DESCRIPTION

Creates an instance of a .Net or COM object. You specify either the type of a .Net class or a Programmatic Identifier (ProgID) of a COM object. By default, you type the fully-qualified name of a .Net class and the cmdlet returns a reference to an instance of that class. To create an instance of a COM object, use the ComObject parameter and specify the ProgID of the object as its value.

 

PARAMETERS

 

-typeName <string>

Specifies the fully-qualified name of the .Net class. You cannot specify both the TypeName parameter and the ComObject parameter.

 

Required?

true

Position?

1

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-argumentList <Object[]>

Specifies a list of arguments to pass to the constructor of the .Net class. Separate elements in the list by using commas (,).

 

Required?

false

Position?

2

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-comObject <string>

Programmatic Identifier (ProgID) of the COM object.

 

Required?

true

Position?

1

Default value

 

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

-strict <SwitchParameter>

Specifies that an error should be raised if the COM object that you attempt to create uses an interop assembly. This enables you to distinguish actual COM objects from .Net objects with COM-callable wrappers.

 

Required?

false

Position?

named

Default value

false

Accept pipeline input?  

false

Accept wildcard characters? 

false

 

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. For more information, type, "get-help about_commonparameters".

 

RETURN TYPE

Object

 

NOTES

 

New-Object provides the most commonly-used functionality of the VBScript CreateObject function. A statement like Set objShell = CreateObject("Shell.Application") in VBScript can be translated to $objShell = new-object -comobject "Shell.Application" in Windows PowerShell.

 

New-Object expands upon the functionality available in the Windows Script Host environment by making it easy to work with .Net objects from the command line and within scripts.

 

For more information, type "Get-Help New-Object  -detailed". For technical information, type "Get-Help New-Object -full".

 

When specifying multiple values for a parameter, use commas to separate the values. For example, "<parameter-name> <value1>, <value2>".

 

EXAMPLE 1

 

$ver = new-object -typename System.Version -argumentlist "1.2.3.4"

$ver | get-member

 

This command creates a .Net object of type System.Version, using the string "1.2.3.4" as the constructor. It stores a reference to the object in the $ver variable and pipes it to the Get-Member cmdlet to display the properties and methods of the referenced object.

 

EXAMPLE 2

 

$ie = new-object -comobject InternetExplorer.Application

$ie.navigate2("www.microsoft.com")

$ie.visible = $true

 

This command creates an instance of the COM object that represents the Internet Explorer application. It uses the object to navigate to a site, and then sets the visible property of the object to $true to make the application visible.

 

EXAMPLE 3

 

$a=new-object -comobject Word.Application -strict

$a.visible=$true

 

This command demonstrates that specifying the Strict parameter will cause new-object to generate a non-terminating

error when the COM object created uses an interop assembly.

 

New-Object : The object written to the pipeline is an instance of the type "Microsoft.Office.Interop.Word.ApplicationClass" from the component's primary interop assembly. If this type exposes different members than the IDispatch members, scripts written to work with this object might not work if the primary interop assembly is not installed.

 

At line:1 char:14

+ $a=New-Object  <<<< -COM Word.Application -Strict; $a.visible=$true

 

EXAMPLE 4

 

$objshell = new-object -comobject "Shell.Application"

$objshell | get-member

$objshell.ToggleDesktop()

 

The command uses the ComObject parameter to create a COM object with the ProgID, "Shell.Application". It stores the resulting reference to the object in the $objShell variable and pipes that variable to the Get-Membercmdlet. The Get-Member cmdlet displays information about the properties and methods of the COM object. In the last line, the ToggleDesktop method of the object is called. It minimizes all of the open windows on your desktop.

 

RELATED LINKS

Compare-Object

Select-Object

Sort-Object

ForEach-Object

Group-Object

Measure-Object

Tee-Object

Where-Object