COM pt 1

In April I will be doing a User Group Live Meeting on using COM with PowerShell.  It is such a wide topic I thought I’d start a few posts to set some background – it also helps my reasearch

COM is the way applications were built for the Windows platform before .NET appeared.  Ugh pre-historic stuff that no one cares about I hear you shout.

Opps. Not really

COM is still around a happily surviving today. For instance the Office applications, IE and the Windows Scripting Host functionality are all COM based

We can discover some information about the COM based applications installed in our systems by using

Get-WmiObject -Class Win32_COMApplication | sort Name | Select Name

This will generate a very long list – don’t be surprised if the early entries have a blank name. Unfortunately the name doesn’t always reflect what we need to use to work with the COM object.

For instance the list of applications generates these examples

Shell ChkdskEx Dialog
Shell Computer Accounts
Shell Computer Groups
Shell Execute Hardware Event Handler
Shell FMIFS Wrapper
Shell Hardware Mixed Content Handler
Shell Hardware Mixed Content Handler Cancelled
Shell Security Editor
ShellWindows

 

if we search for ShellWindows for instance  -  http://msdn.microsoft.com/en-us/library/bb773974(VS.85).aspx  - we see that it represents a collection of open windows belonging to the shell. In many cases COM objects are based on a hierarchy and we have to start at the top of hierarchy.

If you work around in the documentation you will often find a topic that describes the scripting interface or scripting objects. Scripting is somewhere in the title e.g. http://msdn.microsoft.com/en-us/library/bb773177(v=VS.85).aspx

 

If we hunt around in the list we find the shell object which “Represents the objects in the Shell. Methods are provided to control the Shell and to execute commands within the Shell. There are also methods to obtain other Shell-related objects.”

 

This sounds promising so we follow it to http://msdn.microsoft.com/en-us/library/bb774094(v=VS.85).aspx where we find in the properties section a reference to the objects’s Application object.  Bingo – just what we want.

Unfortunately we don’t get much in the way of examples so we have to go back to what we can work out. If you have seen any COM based PowerShell you know that we use the New-Object cmdlet and if you look in the help you find this

$shell = New-Object -ComObject "Shell.Application"

This structure is what we will often see – the name of the object followed by .Application.  If we are working with Word it becomes

$word = New-Object -ComObject "Word.Application"

Let’s go back to the Shell and find out what it does

$shell | gm

It has methods and properties just like .NET so what do we have

$shell.TileVertically()

is fun

$shell.TileHorizontally() supplies a bit of amusement but don’t have too many Windows open when you use them otherwise you will spend a while sorting them out

Some of the other methods look useful so we’ll explore them later

Leave a Reply