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 PowerShell’s Shell.Application
- PowerShell’s New-Object -Com
- PowerShell Script Manipulating the Windows Explorer
- Research Shell.Application Methods and Properties
- PowerShell 3.0 New-Object
- Summary of Com Shell Objects
♣
PowerShell’s 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) Assuming You Have Installed PowerShell
Using the ISE (GUI) or the PowerShell command line issue these commands:
2) Create the object (Shell.Application)
The first step is to create an object and assign it to a variable, for example:
# PowerShell Shell.Application
$ShellExp = New-Object -ComObject Shell.Application
3) Let us put the object to work and open the Windows Explorer at the C:\
# ShellOpen.ps1 Opening Explorer using PowerShell
$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:\.
Guy Recommends: 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
PowerShell Script Manipulating 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 https://computerperformance.co.uk/
# Version 2.5 – November 2013
# Sets the Drive
$Drive = "C:\windows"
# Launches the Explorer
$ShellExp = New-Object -ComObject Shell.Application
$ShellExp.explore($Drive)
Research Shell.Application Methods and Properties
Let us investigate the methods and properties available to our shell object with Get-Member
# PowerShell Shell.Application properties
$ShellExp = New-Object -ComObject Shell.Application
$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.
®
