Converting VBScript's CreateObject Function

Definition: Creates and returns a reference to an Automation object.


In Windows PowerShell you create new COM objects by using the New-Object Cmdlet. To do so call New-Object, passing the Cmdlet the -comobject parameter, which tells Windows PowerShell to create a new COM object rather than, say, a new .NET Framework object. The -comobject parameter is then followed by the ProgID of the COM object.

For example, the following two commands create an instance of Microsoft Excel and then, just to prove that the first command did create an instance of Excel, the second command makes the application visible onscreen:

$a = new-object -comobject Excel.Application
$a.visible = $True

Incidentally, when using the New-Object Cmdlet you might want to add the -strict parameter, like so:

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

That helps ensure that you are working with a true COM object and not a COM wrapper around a .NET object.

Return to the VBScript to Windows PowerShell home page