1

I've followed all the examples in these threads but something has changed in the latest update it seems.

Close Explorer window by varying ID or filename

Closing All Explorer Windows in PowerShell

How to close all windows

My windows version is: Version: 1909 BuildNumber: 18363

I'm stuck with trying to close an explorer window using PowerShell with the following code:

$myFolder = "C:\Program Files"
$shell = New-Object -ComObject Shell.Application
$window = $shell.Windows() | Where-Object { $_.LocationURL -eq "$(([uri]"$myFolder").AbsoluteUri)" }
write-output ($window.LocationURL -eq $(([uri]"$myFolder").AbsoluteUri))
                                 # write-output $window.AbsoluteUri
                                 # write-output ($window.HWND)
                                 # write-output ($window.LocationURL)
                                 # Get-Member -InputObject $window.Parent()
Get-Member -InputObject $window  # prints out the methods associated with the window
$window.Refresh()                # this method works
$window.Quit()                   # this method doesn't
$window.GoBack()                 # this method works
$window.Parent().Quit()          # this doesn't
                                 # $window | ForEach-Object { $_.Quit() }

If you open up C:\Program Files you'll see that the methods described in the code work but the Quit method doesn't.

Ste
  • 1,729
  • 1
  • 17
  • 27
  • 1
    I'm running the same Win10 build as you and both the quit methods worked as expected. Must be something local to your computer, try disabling your Antivirus software for example. Could you update your question with any errors you get when you try either quit method? – jfrmilner Jan 18 '20 at 20:43
  • To confirm @jfrmilner's findings: It works for me too; with File Explorer windows open, `@((New-Object -Com Shell.Application).Windows()).Quit()` successfully closes them all. – mklement0 Jan 19 '20 at 01:53
  • Make sure that there are no other IE sessions open before you start your script: check task manager or logoff/logon before you test. – iRon Jan 19 '20 at 08:59
  • I will try to enable sandbox in Windows to test this further and run in that mode tomorrow. I'll report back then. `@((New-Object -Com Shell.Application).Windows()).Quit()` didn't work. I use MS virus protection but I'll sandbox Windows and work from there. – Ste Jan 19 '20 at 19:54
  • The code I posted in the question works perfectly in the sandboxed environment so there much be an issue with my install. `@((New-Object -Com Shell.Application).Windows()).Quit()` also works too. Thanks guys. I'll try hunt down this issue. – Ste Jan 23 '20 at 17:05

0 Answers0