PowerShell New-Object -ComObject

Scripting COM Shell Objects – Launch Office Programs

I will show you how to use PowerShell’s New-Object to create 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.

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 namespaces 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.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

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

PowerShell Script to Explore with the Windows Explorer

The idea behind a second version of opening the Windows Explorer is to give you perspective.  By changing a few items, I hope that it gives you extra understanding, also more ideas for your own situation.  In the example below I have introduced a variable $Drive to hold the value for the folder, which you want explorer to view.  Note also how I have changed .open("D:") to .explore("C:\windows").  For this script to work, you need to have a \windows folder on your c: drive, fortunately, this is the default location for this system folder.

# ShellExplore.ps1
# Opening Explorer using PowerShell
# Author Guy Thomas https://computerperformance.co.uk/
# Version 2.5 – October 2010
# Sets the Drive
$Drive = "C:\windows"
# Launches the Explorer
$ShellExp = New-Object -ComObject Shell.Application
$ShellExp.explore($Drive)

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Research Shell.Application  Methods and Properties

Let us investigate the methods and properties available to our shell object with Get-Member

# PowerShell Shell.Application properties
$ShellExp = New-Object -ComObject Shell.Application
$ShellExp | Get-Member

In particular, lookout for the methods: ‘Open’ and ‘Explore’, because these are the methods that we are going to apply to our object.

®

Name MemberType
——————- ———-
AddToRecentMethod
BrowseForFolder Method
CanStartStopService Method
CascadeWindows Method
ControlPanelItem Method
EjectPC Method
Explore Method
ExplorerPolicy Method
FileRun Method
FindComputer Method
FindFiles Method
FindPrinterMethod
GetSetting Method
GetSystemInformation Method
Help Method
IsRestricted Method
IsServiceRunning Method
MinimizeAll Method
NameSpace Method
Open Method
RefreshMenu Method
ServiceStart Method
ServiceStopMethod
SetTime Method
ShellExecute Method
ShowBrowserBar Method
ShutdownWindows Method
Suspend Method
TileHorizontally Method
TileVertically Method
ToggleDesktop Method
TrayProperties Method
UndoMinimizeALL Method
WindowsMethod
WindowsSecurity Method
Application Property
Parent Property

ComObject in PowerShell 3.0 Logon Script

The purpose of this script is to map a network drive in PowerShell.  Observe how the ComObject can also act as a wrapper for the WScript.Network methods.

# PowerShell v 3 Logon Script Example
$Net = $(New-Object -ComObject WScript.Network)
$Net.MapNetworkDrive("P:", "\\YourMachine\Stuff")

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. 

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

 


See more Microsoft PowerShell tasks:

PowerShell Home   • Shell Application   • New-Object   • PowerShell Add Printer   • PowerShell -com

PowerShell Logon Script  • Map Network Drive  • PowerShell Create Shortcut  • Free CSV Import Tool

Invoke-Expression   • Invoke-Command   • Invoke-Item   • PowerShell Expression v Command Mode

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.