PowerShell; Use COM to interact with other applications

If we want to move beyond the abilities afforded to PowerShell via its module system; we’ll eventually need to interact with other applications in our own code. For example; have you ever tried to load the “Outlook” module for PowerShell? There isn’t one, but does that mean we can’t interact with the software? Of course not. PowerShell can interact with applications in a myriad of ways, you’ve heard all the buzz words; “REST API”, “SOAP API”, “COM” but how do we use these tools? What are they really? In simplest terms they’re what us old school techs call “hooks”, built-in ways for applications to interact and exchange data. PowerShell is capable of using all of the popular methods including REST with JSON but in this episode we’ll discuss COM.

The Component Objet Model (COM) was introduced by Microsoft in 1993 as a non language specific standard for inter-process communication. It serves as the basis for some of Microsoft’s most popular technology; OLE, Active X, Direct X, and the Windows Runtime are all directly based on it. Using COM can be both extremely simple and frustratingly complex depending on what you are trying to accomplish but since nobody is going to read a 200 page blog post we’ll start with the easy stuff. If you would like a more detailed explanation of COM refer to https://msdn.microsoft.com/en-us/library/windows/desktop/ms680573(v=vs.85).aspx.

All of the examples and code that follows interacts with Outlook, I am an email admin after all, the techniques work with pretty much any application that runs on Windows. First we need to create an object to hold our application in memory while we work with it. How do you hold something in memory? With a variable of course, $outlook = new-object -com outlook.applicationwill essentially open a hidden copy of Outlook and store it in the $outlook variable so that we can send commands and exchange data with it (Get it? Outlook, Exchange data, I’m so funny). How does one find out what commands you can send to a COM object? You might be tempted to try help and that’s a good instinct but we’ve actually switched to C# here (called encapsulation) so PowerShell’s help won’t know anything about Outlook. We need to ask PowerShell to ask the Outlook COM object what commands (methods) it has available. We use the Get-Member command to do that; new-object -com outlook.application|gm will return a list of all the options at our disposal. Note that we had to create the object before we can get its member methods.

 I always try to keep these posts short enough to read and obviously we could spend the next year just going over all the different options in Outlook alone so lets look at a specific scenario. We’re writing a script/application and want our user’s Outlook contacts to pop-up on the screen.

$outlook = new-object -com outlook.application
$contacts = $outlook.session.GetDefaultFolder(10)

In the short script above we have loaded our application into memory, then selected the contacts folder. I just happen to know that each Outlook folder type (calendar, inbox, contacts, appointments, etc.) has an assigned number, GetDefaultFolder is saying get the top folder of this type and the display method is opening the selected item on the user’s screen.

Just for fun, lets create an Excel spreadsheet. Just to prove we can.

$Excel = New-Object -com Excel.Application
$Excel.Visible =$True
$Workbook = $Excel.workbooks.add()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s