#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
© 2018 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: