3

#PSTip Use Shell.Application to determine which folder is open in File Explorer

By using the Windows() method and the LocationURL and LocationName properties we can programmatically determine which folder is open in File Explorer.

$ShellApp = New-Object -ComObject Shell.Application
$ShellApp.Windows() | Where-Object {$_.Name -eq 'File Explorer'} | 
Select-Object LocationName,LocationURL  

The Where-Object statement is required to filter out results from Internet Explorer. If you are interested in all results including the open Internet Explorer windows and tabs the Where-Object statement can be omitted.

Filed in: Tips and Tricks

3 Responses to "#PSTip Use Shell.Application to determine which folder is open in File Explorer"

  1. James Bernie says:

    On a 2008 R2 server you look for “Windows Explorer”, 2012 switches over to “File Explorer”.

    This was very handy for a tool I have been working on so I don’t open a new instance of Explorer if one is already open at a specific folder path.

    thanks!

Leave a Reply

Submit Comment

© 2017 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: