Close File Explorer Window

Page 1 of 2 12 LastLast

  1. Posts : 841
    Windows 10 Pro 21H2
       #1

    Close File Explorer Window


    Using Taskkill to close File Explorer ends some programs running in System Tray, because it closes the Desktop, while Ccleaner can close it without ending any programs, how can this be done with a batch file?
      My Computer


  2. Posts : 5,452
    Windows 11 Home
       #2

    You can restart it ASAP, but I guess that is not exactly, what you want.
    Code:
    taskkill /im explorer.exe /f & explorer.exe
    Not sure, how CCleaner does it, but you can try Process Monitor to find out. I do not want that junk in my PC.
      My Computer


  3. Posts : 841
    Windows 10 Pro 21H2
    Thread Starter
       #3

    TairikuOkami said:
    You can restart it ASAP, but I guess that is not exactly, what you want.
    Code:
    taskkill /im explorer.exe /f & explorer.exe
    Not sure, how CCleaner does it, but you can try Process Monitor to find out. I do not want that junk in my PC.
    It appears that this can only be done with compiled code using sc_close - bummer.....
      My Computer


  4. Posts : 989
    Microsoft Windows 10 Home
       #4

    Don't know of a batch way, but simple in PowerShell:
    Code:
    $shell = New-Object -ComObject Shell.Application
    foreach ($window in $shell.windows())
        {$window.quit()}
    Keith
    Last edited by KeithM; 05 Apr 2019 at 17:33. Reason: Correcting code
      My Computer


  5. Posts : 841
    Windows 10 Pro 21H2
    Thread Starter
       #5

    KeithM said:
    Don't know of a batch way, but simple in PowerShell:
    Code:
    $shell = New-Object -ComObject Shell.Application
    foreach ($window in $shell.windows())
        {$window.close}
    Keith
    I am not familiar with this, but would it not close all open windows due to "foreach"? I am looking to close Explorer only.
      My Computer


  6. Posts : 989
    Microsoft Windows 10 Home
       #6

    LOL. Just tested & I mis-typed. If you'd run the code you would have gotten PowerShell errors and no windows closed! Earlier post now corrected.

    You can have multiple file explorer windows open. The shell.windows collection consists solely of File Explorer windows, not all application windows. If you're nervous, run this code first to see what is in the windows collection:

    Code:
    $shell = New-Object -ComObject Shell.Application
    foreach ($window in $shell.windows())
        {$window.locationname}
    Keith
      My Computer


  7. Posts : 841
    Windows 10 Pro 21H2
    Thread Starter
       #7

    KeithM said:
    You can have multiple file explorer windows open. The shell.windows collection consists solely of File Explorer windows, not all application windows. If you're nervous, run this code first to see what is in the windows collection:

    Code:
    $shell = New-Object -ComObject Shell.Application
    foreach ($window in $shell.windows())
        {$window.locationname}
    Keith
    This code did list the Explorer windows, but I ran the "close" shell as Administrator with two Explorer windows open and it did not close either.
      My Computer


  8. Posts : 989
    Microsoft Windows 10 Home
       #8

    Just edited my original post $window.close was incorrect. Correct method is $window.quit().

    Keith
      My Computer


  9. Posts : 841
    Windows 10 Pro 21H2
    Thread Starter
       #9

    KeithM said:
    Just edited my original post $window.close was incorrect. Correct method is $window.quit().

    Keith
    I did not get yours' to work, but with your suggestion, I did find this one:
    Code:
    $a = (New-Object -comObject Shell.Application).Windows() |  ? { $_.FullName -ne $null} |
     ? { $_.FullName.toLower().Endswith('\explorer.exe') } 
    
     $a | % {  $_.Quit() }
      My Computer


  10. Posts : 989
    Microsoft Windows 10 Home
       #10

    The best of both our code versions, a one liner!
    Code:
    (New-Object -ComObject Shell.Application).Windows() | %{$_.quit()}
    Keith
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 10:31.
Find Us




Windows 10 Forums