How to close Windows Explorer from CMD

23,102

Solution 1

Not from a batch file unless you want to write your own command line application that opens up a windows explorer window, and (this is the key part) somehow knows the window handle of that explorer window, so it can post a WM_CLOSE message to it, which basically simulates someone closing that window.

How you would determine "all the explorer windows that got opened" would be that instead of just starting explorer.exe instances from a command line you would do it from your own application.

I think that determining the window handle (HWND in win32 api terms) and posting a close message would be better than trying to track process handles and terminating explorer process instances, since that could cause some side effects beyond those that you'd want.

Solution 2

Close the explorer windows by killing the explorer process (note that this may do more than just kill the windows, but it will definitely do that):

for example, use win+r and try this

cmd /c "taskkill /f /im explorer.exe && start explorer"

If you kill explorer without restarting it, use Ctrl+shift+Esc to pull up the taskmanager and start a new task "explorer".

Share:
23,102
John Boe
Author by

John Boe

Updated on July 15, 2022

Comments

  • John Boe
    John Boe 11 months

    Is it possible to close Windows Explorer from CMD? I have a batch that does this: it will change directory, open explorer in this folder, than run a program. After the user closes the program the batch should close the explorer (or all explorers opened), continue on next folder (cd folder), run the same program in this folder and so on. Till the last folder is processed.

  • John Boe
    John Boe about 11 years
    I don't know what is it window handle. "unless you want to write your own command line application that opens up a windows explorer window" - Yes it would be ideal. But I know nothing about these things.
  • Warren  P
    Warren P about 11 years
    This is a programming questions website. Your question is on topic, if you pick a programming language. But try something yourself first. Lots of people could help you if you asked about how to open an explorer window and find or remember the handle for the window that got opened, either from Python, C++, C#, Delphi, or Ruby. Which one do you know or want to learn?
  • John Boe
    John Boe about 11 years
    Forget about that. I thought CMD could do that job.