Close a particular opened folder using cmd or batch file with Window version 1909 or greater?

As per this thread, it seems that the solution doesn't work anymore for version 1909.

When I open a folder in File Explorer such as C:\ and run the cmd command tasklist /V /FI "IMAGENAME eq explorer.exe" I get NA under the window title column.

And if I try taskkill /FI "IMAGENAME eq explorer.exe" /FI "WINDOWTITLE eq C:\" it doesn't kill File Explorer with C:\ folder open.

Is anyone else experiencing this issue also?


The related thread that no longer works for my build of windows:

How to close a particular opened folder using cmd or batch file?


Solution 1:

TL:DR As far as I can tell this is simply not longer possible at all, due to a redesign Microsoft has made in the latest Windows 10 versions.

The long story:
It looks to me as if in order to enable tabbed/docked windows (each tab holding information of different Explorer sub-processes) Microsoft had to split the windows/display-part off from the actual Explorer-process and now seems to have a single process that handles all the windows/tabs and the actual FileExplorer processes display their output by sending it to that window-manager process.
To complicate matters further you have no way of telling from your batch-file if the user decided to keep the window as a standalone window or docked it as tab to another window.
Tabs are technically a different kind of display-element, with a different close-method.

Killing the window-manager process will of course still close the window/tab, but will drag along everything else associated with it.
I actually tried that a couple of times and the results were "interesting":
Depending on what other software was open at the time either Explorer.exe crashed and had to be restarted (bring up Task Manager with Ctrl-Alt-Del and use the Run menu to relaunch Exporer.exe).
Or Explorer.exe hung in such a bad way nothing worked anymore and I had to power-cycle the computer.

It seems that now the only way to do this corectly is to write a piece of software that runs through all open windows and tabs to identify the one associated with the folder (probably has to be done on basis of the window or tab title) and when it has found it sends the Windows-Close or Tab-Close event to that windows or tab.
And most likely this can only be done if this program also runs with elevated privileges (admin rights) because it needs to poke around in system-structures that most likely require a higher priviledge level then that a regular user normally has.

Solution 2:

If you're open to other platforms, AutoHotkey has typically allowed a person to run a program and capture a PID for the program it starts.

You may also benefit from finding a PPID (parent's PID). e.g., from CMD.exe:

WMIC /APPEND:output.txt PROCESS WHERE "Caption='explorer.exe'" GET Caption,CommandLine,Description,ExecutablePath,Name,ParentProcessId,ProcessId /FORMAT:LIST

(The /APPEND output.txt is optional. Remove if undesired. Also, remove some items after GET if there is too much redundancy. Or remove everything between GET and /FORMAT to show more info.)

Note that if you use PowerShell, those commas will likely be problematic, so place a back-quote/backtick (unshifted tilde, above tab and left of 1) before each comma so PowerShell doesn't interpret the commas for its own use.

WMIC PROCESS WHERE "Caption='explorer.exe'" GET Caption`,CommandLine`,Description`,ExecutablePath`,Name`,ParentProcessId`,ProcessId /FORMAT:LIST

If you can somehow track the ParentProcess ID to match the program that you're using to run Explorer.exe, I suspect that may be a very specific way to close the right window (even more proper/safe than making a guess based on the title bar). However, right now I can't give you a full description of how to do this because I don't have enough details for your setup. (And, even with more details, it might not be feasible for me to completely solve this for you, so I'm just hoping these pointers might be useful resources for you.)