Windows PowerShell


Scripting COM Shell Objects - Launch Windows Explorer

Scripting COM Shell Objects - Launch Windows Explorer

On this page I will show you how to create a COM object, which opens and then manipulates Windows Explorer.  ComObject, or plain COM, is a key PowerShell command that performs many of the jobs previously undertaken by VBScript.  For our task, we are going to persuade PowerShell to create a Shell.Application; from there we will manipulate the Explorer programmatically.

Topics for COM Objects

New-Object -com

All COM objects are created through the command: New-Object -COM.  There are dozens of options and possibilities for New-Object -COM, for our purpose we specifically need a Shell.Application type of object.  Let me take you step-by-step through the method.

1) Create the object (Shell.Application)
The first step is to create an object and assign it to a variable. For example:
$ShellExp = new-object -comObject Shell.Application

2) Object Methods and Properties
Let us investigate the methods and properties available to our shell object:
$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
------------------- ----------
AddToRecent Method
BrowseForFolder Method
CanStartStopService Method
CascadeWindows Method
ControlPanelItem Method
EjectPC Method
Explore Method
ExplorerPolicy Method
FileRun Method
FindComputer Method
FindFiles Method
FindPrinter Method
GetSetting Method
GetSystemInformation Method
Help Method
IsRestricted Method
IsServiceRunning Method
MinimizeAll Method
NameSpace Method
Open Method
RefreshMenu Method
ServiceStart Method
ServiceStop Method
SetTime Method
ShellExecute Method
ShowBrowserBar Method
ShutdownWindows Method
Suspend Method
TileHorizontally Method
TileVertically Method
ToggleDesktop Method
TrayProperties Method
UndoMinimizeALL Method
Windows Method
WindowsSecurity Method
Application Property
Parent Property

3) PowerShell script to actually Open (launch) the Explorer  

Instructions:

  1. Save the code as a file, make sure that you create a .ps1 extension, for example ShellExplore.ps1
  2. Launch PowerShell
  3. Navigate to the folder where you saved the .ps1 file
  4. At the PS> prompt type:  .\ShellExplore (dot backslash filename)

# ShellOpen.ps1
# Opening Explorer using PowerShell
# Author Guy Thomas http://computerperformance.co.uk/
# Version 1.3 - November 2007

# Launches the Explorer
$ShellExp = new-object -comObject Shell.Application
$ShellExp.open("C:\")

Learning Points

When I first experimented with this command I tried $ShellExp.open without the brackets - wrong.  Then I tried $ShellExp.Open() - no good.  Finally I remembered that the parenthesis style of brackets needs to enclose a value, $ShellExp.Open("C:\").  Eureka, success, the Windows Explorer launched anchored at the C:\.

4) 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 http://computerperformance.co.uk/
# Version 2.5 - November 2007
# Sets the Drive
$Drive = "C:\windows"
# Launches the Explorer
$ShellExp = new-object -comObject Shell.Application
$ShellExp.explore($Drive)

  ˚

Summary of -COM Shell objects

Once you have discovered the straightforward technique of creating com objects, then you can specialise by creating a Shell.Application object.  After you have assigned the object to a variable, you can apply methods to perform useful tasks such as opening folders or exploring with Windows Explorer.  The secret of this method is adding a value in the brackets at the end of the command, for example, $ShellExp.Open("C:\").

See Also PowerShell Tutorials:

PowerShell Home  • Com  • Shell Application  • Active Directory  • QAD Snap-in  • Get-Member

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.