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.

The New-Object cmdlet allows you to create a new instance of a .Net or COM object, PowerShell also allows you to create custom objects for your own collections of data items.

Custom objects (PSCustomObject)

A simple custom object of type PSCustomObject can be created using a hash table:

$gyro = [PSCustomObject]@{
    Name = "Gyroscope"
    Id = 64
    DeliveryDate = Get-Date
    OrderDate = '2021-12-01 12:00:00' -as [DateTime]
}

$gyro

A PSCustomObject will accept any data type, you can create something as [Decimal] or [DateTime] but then still update it later to a string value.

If you already have a hash table, it can be converted into a custom Object using the -TypeName of PSObject:
$myObject = New-Object -TypeName PSObject -Property $myHashtable
However it is faster to just directly cast it:
$myObject = [PSCustomObject]$myHashtable

An alternative is to create this using a type-safe template class:

class Asset
{
    [string]$Name = "Gyroscope"
    [int]$Id = 64
    [DateTime]$DeliveryDate = $(Get-Date)
    [DateTime]$OrderDate = $('2021-12-01 12:00:00' -as [DateTime])

}
# Then instantiate and display an object based on this class:
$gyro = [Asset]::new()

$gyro

Examples

Speak a sentence using System.Speech:

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

Speak a sentence using Microsoft Speech SpVoice Interface API:

PS C:\> $voice = new-object -ComObject "SAPI.SPVoice"
PS C:\> $voice.speak("SS64 taught me everything")

System.Speech.* is the "official" support for speech in the .NET framework. SpVoice has the advantage of allowing you to install and use additional voices.

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 PowerShell Cmdlets

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.
Word - How to automate MS Word in PowerShell using a COM object.
Where-Object - Filter input from the pipeline allowing operation on only certain objects.
MacOS Equivalent: say - Convert text to audible speech.


 
Copyright © 1999-2024 SS64.com
Some rights reserved