New-Object

Create an instance of a .Net or COM object. This allows you to startup and control other applications (including VBScript) from powershell.

Syntax
      New-Object [-typeName] string[] [[-argumentList] Object[]]
            [CommonParameters]

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

Key
   typeName string
       The fully-qualified name of the .Net class. 

   -argumentList Object
       A comma separated list of arguments to pass to the constructor of the .Net class.

   -comObject string
       Programmatic Identifier (ProgID) of the COM object.

   -strict 
       Raise an error 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.
	   
   CommonParameters:
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutVariable. 

Examples

Create a COM object "Shell.Application" and store the resulting reference in a variable, display the properties and methods of the COM object (via get-member.) Then use the ToggleDesktop method to minimizes all open desktop windows:

PS C:\>$objShell = new-object -comobject "Shell.Application"
$objShell | get-member
$objShell.ToggleDesktop()


Create a VBScript COM object, start Word for Windows and type in some text using SendKeys:

$objVBscript = new-object -comobject wscript.shell
[void] $objVBscript.popup("This is VBScript",0,"SS64 MsgBox",1)
 
[void] $objVBscript.Run("Winword.exe")
Start-sleep 3
If ($objVBscript.AppActivate("Microsoft Word"))
{
  Start-sleep 2
  $objVBscript.SendKeys("Hello 123")
}

Create a COM object "Word.Application" and store the resulting reference in a variable:

PS C:\>$objWord=new-object -comobject Word.Application
$objWord.visible=$true

Send email:

# Instantiate an SmtpClient object
$objMailClient = new-object Net.Mail.SmtpClient -arg "mailserver.example.com"

# Instantiate a new MailMessage object, with sender, destination,subject and body
$objMessage = new-object Net.Mail.MailMessage("me@source.com","you@destination.com", "Subject", "Here is some email")

# Add an attachment to the message
$objAttach = new-object Net.Mail.Attachment("c:\\demo.txt")
$objMessage.Attachments.Add($objAttach)

# Send the message object using the email client
$objMailClient.Send($objMessage)

For more examples, type: "get-help New-Object -detailed"

"The creation of a thousand forests is in one acorn" - Ralph Waldo Emerson

Related Powershell Commands:

compare-object Compare the properties of objects
ForEach-object - Loop for each object in the pipeline
group-object - Group the objects that contain the same value for a common property
measure-object - Measure aspects of object properties and create objects from those values
New-Item - Create a new item in a namespace (New File/New Folder)
select-object - Select objects based on parameters set in the Cmdlet command string
sort-object - Sort the input objects by property value
tee-object - Send input objects to two places
where-object - Filter input from the pipeline allowing operation on only certain objects



Back to the Top

© Copyright SS64.com 1999-2010
Some rights reserved