Using COM with Windows PowerShell 1.0
In previous chapters we have have covered the use of the .NET framework from within Windows PowerShell. In this chapter we will look at leveraging Microsoft's Component Object Model (COM) from with Windows PowerShell. In particular the issue of launching and interacting with applications and working with the Windows desktop from whithin PowerShell will be covered.
Listing Available COM Objects
Available COM objects are listed in the Windows registry. A complete list may be obtained from within Windows PowerShell by executing the following function:
function com_list { $path = "REGISTRY::HKey_Classes_Root\clsid\*\progid" foreach ($val in dir $path) { $val.getvalue("") } }
The above script will provide an extensive list of the COM objects registered in the Windows registry. A search for a specific match may be performed by piping the output of the function through to the select-string cmdlet. For example, the following command lists any COM objects names containing the word explorer:
PS C:\tmp> com_list | select-string explorer InternetExplorer.Application.1 Shell.Explorer.2 Shell.Explorer.1
As illustrated above, there are three entries in the registry which match the criteria. One of these is Internet Explorer and the others relate to Windows Explorer.
Creating COM Object Instances in Windows PowerShell
New COM object instances are created within Windows PowerShell using the new-object cmdlet combined with the -comobject parameter. This parameter may also be abbreviated to -com. In order to avoid a any ambiguity which may result in a .NET or interop library with the same name being loaded, the -strict switch is also recommended:
new-object -comobject object name -strict
For example, to create new InternetExplorer.Application object:
PS C:\tmp> $iexplore = new-object -com InternetExplorer.Application
Listing the Properties and Methods of a COM Object
Once a new instance of a COM object has been created, it is often useful to find out the methods and properties available for that object. As with .NET objects, this can be achieved using the get-member cmdlet (also available via the gm alias). In the following example, this approach is used to identify the methods and properties of the InternetExplorer.Application object instance created in the previous section of this chapter:
PS C:\tmp> $iexplore | gm TypeName: System.__ComObject#{d30c1661-cdaf-11d0-8a3e-00c04fc9e26e} Name MemberType Definition ---- ---------- ---------- ClientToWindow Method void ClientToWindow (int, int) ExecWB Method void ExecWB (OLECMDID, OLECMDEXECOPT, Variant, Variant) GetProperty Method Variant GetProperty (string) GoBack Method void GoBack () GoForward Method void GoForward () GoHome Method void GoHome () GoSearch Method void GoSearch () Navigate Method void Navigate (string, Variant, Variant, Variant, Variant) Navigate2 Method void Navigate2 (Variant, Variant, Variant, Variant, Variant) PutProperty Method void PutProperty (string, Variant) QueryStatusWB Method OLECMDF QueryStatusWB (OLECMDID) Quit Method void Quit () Refresh Method void Refresh () Refresh2 Method void Refresh2 (Variant) ShowBrowserBar Method void ShowBrowserBar (Variant, Variant, Variant) Stop Method void Stop () AddressBar Property bool AddressBar () {get} {set} Application Property IDispatch Application () {get} Busy Property bool Busy () {get} Container Property IDispatch Container () {get} Document Property IDispatch Document () {get} FullName Property string FullName () {get} FullScreen Property bool FullScreen () {get} {set} Height Property int Height () {get} {set} HWND Property int HWND () {get} Left Property int Left () {get} {set} LocationName Property string LocationName () {get} LocationURL Property string LocationURL () {get} MenuBar Property bool MenuBar () {get} {set} Name Property string Name () {get} Offline Property bool Offline () {get} {set} Parent Property IDispatch Parent () {get} Path Property string Path () {get} ReadyState Property tagREADYSTATE ReadyState () {get} RegisterAsBrowser Property bool RegisterAsBrowser () {get} {set} RegisterAsDropTarget Property bool RegisterAsDropTarget () {get} {set} Resizable Property bool Resizable () {get} {set} Silent Property bool Silent () {get} {set} StatusBar Property bool StatusBar () {get} {set} StatusText Property string StatusText () {get} {set} TheaterMode Property bool TheaterMode () {get} {set} ToolBar Property int ToolBar () {get} {set} Top Property int Top () {get} {set} TopLevelContainer Property bool TopLevelContainer () {get} Type Property string Type () {get} Visible Property bool Visible () {get} {set} Width Property int Width () {get} {set}