Scripting With PowerShell COM Objects

Introduction to Scripting COM Objects with PowerShell

ComObject, or plain COM, increases the range of PowerShell activities.  One way of looking at COM objects is as a mechanism for PowerShell to launch programs, for example, mimicking the RUN command.  Another way of looking at ComObjects is performing the role previously undertaken by VBScript.  For both those tasks, scripting with COM objects gives you a rich selection of options.  The bonus of using PowerShell rather than VBScript is that you need fewer commands.

Topics for PowerShell COM Objects


PowerShell New-Object -Com

The secret of manipulating COM objects is starting with the command: New-Object  -COM.  What comes next depends on which type of object you need.  Here are examples of creating, then manipulating ComObject with $variables:- 

Instructions and Assumptions

  1. If necessary get your copy of PowerShell and .Net Framework
  2. Launch Windows PowerShell
  3. Type these commands at the PS> Prompt (or copy and paste my examples)

1) MapNetworkDrive

# PowerShell ComObject and MapNetworkDrive
$net = New-Object -ComObject WScript.Network

Note 1: Naturally, you need to amend \\ server\share to the name of a real UNC share on your network.

Note 2: To see if your PowerShell script performs as planned, launch Windows Explorer.

Note 3: See here for a modern method using New-PSDrive.

2) Internet Explorer

#ComObject to launch the Internet Explorer application.
$ie = New-Object -ComObject InternetExplorer.Application; $ie.visible=$true

Note 4: Incidentally, -Com and -ComObject appear to be interchangeable.

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

New-Object -Com | Get-Member

Let us investigate the properties of the -Com object.  At first, Get-Member appears not work, even help seems unsupportive.  Fortunately, all that is missing is the name of the -Com object that you wish to research.  For example:

#PowerShell New-Object -Com properties
New-Object -Com | Get-Member.


Name       MemberType (Method, Property)
—-         ———- ———-
AddPrinterConnection Method
AddWindowsPrinterConnection Method
EnumNetworkDrives Method
MapNetworkDrive Method
RemoveNetworkDrive Method
SetDefaultPrinter Method
ComputerName Property
Organization Property
Site Property string
UserDomain Property
UserName Property
UserProfile Property

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

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 PowerShell 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

Selection of -Com Applications

Create objects with New-Object -Com xyz.Application

1) Shell.Application
For example:

$shell = New-Object -ComObject Shell.Application  See more on Shell.Application

2) Alternative technique Invoke-Expression

Before you go any further with -ComObject and cmd.exe you may wish to try invoke-Expression.

3) WScript.Shell
For example:

$WSH = New-Object -Com WScript.Shell

Instead of "Explorer", substitute "Excel", "Word", "Calc" or any other registered application.

4) MSScriptControl.ScriptControl
VBscript example:

$VBScript = New-Object -Com MSScriptControl.ScriptControl
$VBScript.language = "vbscript"
$VBScript | Get-Member

5) InputBox() Example

$VBScript = New-Object -Com MSScriptControl.ScriptControl
$VBScript.language = "vbscript"
$VBScript.addcode("function getInput() getInput = inputbox(`"Guy Says Hello`",`"Guy’s box`") End Function" )
$Input = $VBScript.eval("getInput")

Note: This is a complex example because it creates a function, which then does useful work in creating an inputbox().

6) Active Directory – DirectoryServices.DirectoryEntry

Summary of -COM objects

The ComObject family of commands add important capabilities to PowerShell. For example, creating network objects means that you don’t have to revert to VBScript when you need to map network drives. Another way of looking at the -ComObject command is as a PowerShell method of accessing the Run dialog box programmatically.

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.