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
# PowerShell ComObject and MapNetworkDrive
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.
2) Internet Explorer
#ComObject to launch the Internet Explorer application.
Note 4: Incidentally, -Com and -ComObject appear to be interchangeable.
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.
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
Name MemberType (Method, Property)
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.
Create objects with New-Object -Com xyz.Application
$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.
$WSH = New-Object -Com WScript.Shell
Instead of "Explorer", substitute "Excel", "Word", "Calc" or any other registered application.
New-Object -Com MSScriptControl.ScriptControl
5) InputBox() Example
$VBScript = New-Object
Note: This is a complex example because it creates a function, which then does useful work in creating an inputbox().
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:
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.