Windows PowerShell


Scripting COM Objects with PowerShell

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 COM Objects

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

$net = New-Object -com WScript.Network
$net.mapnetworkdrive("Y:","\\server\share")

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.

See here for a more detailed MapNetworkDrive example.

2) Internet Explorer

$ie = New-Object -ComObject InternetExplorer.Application; $ie.visible=$true

Note 1: Incidentally, -com and -ComObject appear to be interchangeable.

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:
New-Object -com wscript.network | 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

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) WScript.Shell
For example:

$WSH = New-Object -com WScript.Shell
$WSH.Run("Explorer.exe")

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

3) MSScriptControl.ScriptControl
VBscript example:

$VBScript = new-object -com MSScriptControl.ScriptControl
$VBScript.language = "vbscript"
$VBScript | get-Member

4) 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().

5) 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.

See more PowerShell real life tasks

PowerShell Home  • Real life tasks  • IpConfig  • Exchange  • Com objects  • Services  • Syntax

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Webcomputerperformance.co.uk

Guy Recommends: SolarWinds Exchange Monitor

Exchange Monitor from SolarWindsHere is a free tool to monitor your Exchange Server

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.