Contact Info

Crumbtrail

ActiveXperts.com » Administration » Powershell » Powershell 1.0 » New-Object

New-Object - Powershell 1.0 CmdLet

ActiveXperts Network Monitor ships with integrated Powershell scripts to monitor complex network. The scripts run out of the box
Download the ActiveXperts Network Monitor FREE version now »

New-Object

Description
Create a new .Net object

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

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

Options
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.

Example(s)
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 COM object "Word.Application" and store the resulting reference in a variable:

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

Send email:

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

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

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

# Send the mail
$SMTP_Mail.Send($Message)

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