PowerShell Team

Monad provides a way to create new com objects with new-object

$ie = New-Object -ComObject InternetExplorer.Application

Great but what about if you want to bind to an existing object?  Where is the equivalent of GetObject()?

This is one of those good new/bad news stories.  First the bad news.  Monad does not provide a Cmdlet that exposes this function.  Alas, to ship is to choose and we are not going to get to in for V1.  Now for the good news.  Monad provides great .NET support and .NET exposes this function.


MSH> $w=[System.Runtime.InteropServices.Marshal]::GetActiveObject(“Word.Application”)

MSH> $w.documents |ft name,path -auto
Name                                              Path
—-                                              —-
RE: Question regarding Word scripting with MSH… RE:
cmdlets as classes.doc                            \\imself-dfs-07\dfsroot\dfsmydocs\js…
Event Schema.doc                                  \\imself-dfs-07\dfsroot\dfsmydocs\js...


Let me be quick to point out a problem that exists in your beta code but is fixed in the upcoming release.  If you just typed $w.Documents – it is going to appear to have hung and you’ll either have to wait a LONG time or hit CTRL-BREAK to terminate the session.  The work around is very simple, pipe the output to Get-Member and use that to find out what properties you want and then ask for those specific properties.

Anyway, this is an example of how Monad’s support for a wide range of technologies (text processing, COM, WMI, ADSI, ADO, XML, .NET, etc) really pays off.  As a rule, we are trying to expose the system with Cmdlet and namespace semantics.  It is going to take a while before the entirety of the system is exposed this way but by providing direct access to these other technologies, it enables the community to provide this support.  For example the function:


Function Get-Object ($ProgID) {


and the following formating directives:




allows us to do the following:


MSH> $w = Get-Object -ProgId Word.Application
MSH> $w.documents
Name                                              Fullname
—-                                        ——–
RE: Question regarding Word scripting with MSH… RE:
cmdlets as classes.doc                            \\imself-dfs-07\dfsroot\dfsmydocs\js…Event Schema.doc                                  \\imself-dfs-07\dfsroot\dfsmydocs\js


Extend the system and Enjoy!

Jeffrey P. Snover
Monad Architect

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

PSMDTAG:TYPE:COM: InternetExplorerer.Application

PSMDTAG:FAQ: What is the equivalent of VBSCRIPT’s GetObject()?  Answer: [System.Runtime.InteropServices.Marshal]::GetActiveObject(“progid”)

PSMDTAG:FORMATEXTENSION: (Word.Application).Documents


Discussion is closed.

Feedback usabilla icon