Ezine 214 PowerShell Manipulates Office Applications

Scripting COM Shell Objects – Launch Office Programs

I will show you how to use PowerShell to create then manipulate Microsoft Office objects such as Word.Application or Outlook.Application.  Incidentally, this PowerShell technique of replaces many of the tasks previously undertaken by VBScript’s CreateObject. 

Topics for PowerShell’s New-Object -comObject

 ♣

PowerShell’s New-Object -comObject

All COM objects are created through the command: New-Object.  There are dozens of applications that you can specify with the -comObject parameter.  For example, Excel.Application or Outlook.Application

1) Create the object (New-Object)
The first step is to create a new object and then assign it to a variable, for example:

# PowerShell Word.Application Create and Launch
Clear-host
$MsApp = New-Object -comObject Word.Application
$MsApp.Visible = $true

Note 1:  Observe how the parameter -comObject handles the application.  Incidentally, -comObject is often abbreviated to -com.

Note 2:  You could substitute Excel for Word, however, Outlook would not work, but see example 2 for a successful technique to see your email.

1a) Closing Word

Clear-Host
$MsApp.Quit()

2) Starting Outlook

Using PowerShell to launch Outlook is a little more difficult than Word or Excel.  Here is a method which gets the namespace then displays the inbox.

# PowerShell Outlook.Application and GetNamespace
Clear-Host
$MsOutlook = New-Object -comObject Outlook.Application
$Namespace = $MsOutlook.GetNamespace("MAPI")
$Folder = $Namespace.GetDefaultFolder("olFolderInbox")
$Explorer = $Folder.GetExplorer()
$Explorer.Display()

Note 3:  For applications such as Outlook you could employ a completely different cmdlet and call for:
Start-Process Outlook.exe.

3) Working With Outlook in PowerShell

# PowerShell Retrieves Outlook Email Subject
Clear-Host
$MsOutlook = New-Object -comObject Outlook.Application
$SentMail = $MsOutlook.Session.GetDefaultFolder(6)
$SentMail.Items | select -last 5 TaskSubject

Note 4: The script connects to Default Folder 6 which corresponds to the inbox, from there it retrieves the last 5 items which are the Subjects of the newest emails.

Other useful Outlook Const values

CONST olFolderDeletedItems = 3
CONST olFolderOutbox = 4
CONST olFolderSentMail = 5
CONST olFolderInbox = 6
CONST olFolderCalendar = 9
CONST olFolderContacts = 10
CONST olFolderJournal = 11
CONST olFolderNotes = 12
CONST olFolderTasks = 13
CONST olFolderDrafts = 16

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Summary of -comObject and Office Applications

We have investigated how to use PowerShell’s New-Object to create Microsoft Office objects such as Excel.Application or Outlook.Application.  Incidentally, this PowerShell technique of replaces many of the tasks previously undertaken by VBScript’s CreateObject. 

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell ‘where‘  • PowerShell ‘Sort’

Windows PowerShell Modules  • Import-Module  • PowerShell Module Directory 

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links.