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 [-comObject] string[] [-strict]  
            [-Property hashtable] [CommonParameters]

      New-Object [-typeName] string[] [[-argumentList] Object[]]
            [-Property hashtable] [CommonParameters]

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

   -Property hashtable
Set property values and invokes methods of the new object. Enter a hash table in which the keys are the names of properties or methods and the values are property values or method arguments. New-Object creates the object and sets each property value and invokes each method in the order that they appear in the hash table. If the new object is derived from the PSObject class, a property is specified that does not exist on the object, it will be added to the object as a NoteProperty. If the object is not a PSObject, the command generates a non-terminating error. -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. -typeName string The fully-qualified name of the .Net class. CommonParameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable, -OutBuffer -OutVariable.

Examples

Speak a sentence:

PS C:\> Add-Type -AssemblyName System.Speech
$say = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
$say.Speak('SS64 taught me everything.')

Open MS Word, 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

Create a VBScript COM object, start MS Word 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 "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 minimize all open desktop windows:

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

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@example.com","you@example.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)

Install fonts from local path :

$FONTS = 0x14
$objShell = New-Object -ComObject Shell.Application
$objFolder = $objShell.Namespace($FONTS)
$objFolder.CopyHere("C:\tempfiles\ITCBLKAD.TTF")

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

“The creation of a thousand forests is in one acorn” ~ Ralph Waldo Emerson

Related:

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
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
OS X Equivalent: say - Convert text to audible speech.


© Copyright SS64.com 1999-2013
Some rights reserved