8

I want to get a report of all open explorer windows titles and current paths. The current paths part of this is problem is answered here with C#, but I want this for powershell and am not sure how to adapt it. I am not sure how to bring out the window titles part of it.

Could someone please assist.

Community
  • 1
  • 1
CoderWolf
  • 149
  • 1
  • 6
  • Have you tried anything in powershell? Getting any specific errors? – MichaelChirico Jul 10 '15 at 18:43
  • I am have not tried anything yet. Primarily because I dont know how to use ShellWindowClass with powershell. And due to the fact that Windows explorer embeds all its processes, I cant use get-process to do anything like get-process | echo $_.mainWindowTitle – CoderWolf Jul 10 '15 at 19:28
  • Based on Ansgar's answer: `LocationName` is the property containing name of the Window, as used in the task bar. Using his answer, you can see `LocationName` with: `$app.Windows() | Select-Object LocationURL, LocationName` ... `LocationName` works, as is, for local folders. *(Remote folders also contain the unc name in parentheses (\\ – MikeD Mar 19 '17 at 14:08
  • @MikeD: `LocationName` is the name of the _location_, which may or may not be the same as the _window_ title; somewhat obscurely, it is the `.Document.Folder.Self.Path` property that contains the location's full local or UNC path. – mklement0 Nov 09 '19 at 22:17

2 Answers2

8

Sounds to me like you're looking for something like this:

$app = New-Object -COM 'Shell.Application'
$app.Windows() | Select-Object LocationURL

AFAICS the window objects don't have a title property, but you can get that information from Get-Process via the window handle ID:

function Get-WindowTitle($handle) {
  Get-Process |
    Where-Object { $_.MainWindowHandle -eq $handle } |
    Select-Object -Expand MainWindowTitle
}

$app = New-Object -COM 'Shell.Application'
$app.Windows() |
  Select-Object LocationURL, @{n='Title';e={Get-WindowTitle $_.HWND}}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • This is one more step on the process. If I am inferring properly, this give me the ShellWindowsClass part of the equation, but I also want to get the application titlebar from it. ie Titlebar LocationURL C:\dir1 file://c:/dir1 C:\dir2 file://c:/dir2 Sidenote: tested LocationName from that area, and have actually added it to my final choice, since some locations may not have a LocationURL (like Devices and printers, etc..) (sorry after multiple edits, I cant seem to get line breaks to work.) – CoderWolf Jul 10 '15 at 20:45
  • Windows explorer has to have the information somewhere, because it is displayed in the taskbar, Unless they just do a conversion between the Location URL format nomenclature to an older standard DOS style nomenclature. PS sorry for the delay, I was off line for a few days. – CoderWolf Jul 13 '15 at 14:18
  • guess I will settle for the half I have for now, – CoderWolf Jul 14 '15 at 20:27
  • 1
    `LocationName` is the property containing name of the Window, as used in the task bar. Using what you have started, you can see this with: `$app.Windows() | Select-Object LocationURL, LocationName` ... `LocationName` works, as is, for local folders. *(Remote folders also contain the unc name in parentheses (\\unc name) which you would need to truncate.) – MikeD Mar 19 '17 at 14:06
  • I'm trying to make this work. I tried "$app.Windows() | Select-Object LocationName", but the output only contains the Explorer Windows folder names, not the full path and folder that is displayed in the Explorer title. I tried "$app.Windows() | Select-Object LocationName ... LocationName" but that gives an error. I'm not sure how you accomplished the code formatting in a comment, but perhaps something was lost in MikeD's last comment? – tim11g Nov 07 '19 at 21:55
  • I also tried the Get-WindowTitle function from further above. That does provide an output, but it is truncated to a column width of 5 characters: "C:..." – tim11g Nov 07 '19 at 21:59
  • My goal is to capture the currently open set of explorer windows, and write out a CMD file with commands like: C:\WINDOWS\explorer.exe /e, "C:\open\this\folder" – tim11g Nov 07 '19 at 22:02
  • @tim11g Please post a question of your own. – Ansgar Wiechers Nov 07 '19 at 22:08
5

Ansgar Wiechers' answer is helpful, but the title of File Explorer windows doesn't necessarily contain the full path of the location (folder) being displayed.

Somewhat obscurely, it is the .Document.Folder.Self.Path property of the window objects returned by the .Windows() method of the Shell.Application COM object that contains the full, local or UNC path.

Therefore, the following lists the full paths of all open Explorer windows:

(New-Object -ComObject 'Shell.Application').Windows() | ForEach-Object { 
  $_.Document.Folder.Self.Path 
}

Note: Special locations such as File Explorer's "Quick access" are represented by ::-prefixed GUIDs; e.g., ::{679F85CB-0220-4080-B29B-5540CC05AAB6}

mklement0
  • 382,024
  • 64
  • 607
  • 775