Closing All Explorer Windows in PowerShell
Asked Answered
S

3

7

I am writing the following code to close all explorer windows with PowerShell:

(New-Object -comObject Shell.Application).Windows() |
 ? { $_.FullName -ne $null} |
 ? { $_.FullName.toLower().Endswith('\explorer.exe') } | % { $_.Quit() }

But it does not close out all the open windows. Instead, it closes only RoundDown(N/2)+1 windows, and leaves RoundUp(N/2)-1 windows open.

Can anyone help with this?

Sleepwalk answered 19/7, 2013 at 19:41 Comment(0)
D
15

I think there's something in the pipeline that goes wrong. This code works:

$a = (New-Object -comObject Shell.Application).Windows() |
 ? { $_.FullName -ne $null} |
 ? { $_.FullName.toLower().Endswith('\explorer.exe') } 

 $a | % {  $_.Quit() }
Duello answered 19/7, 2013 at 20:12 Comment(3)
Classic issue with modifying the collection while you are walking its contents. By killing explorer.exe instances, Windows is updating the collection while you're still walking it. That usually results in badness. :-) One common work around is to walk the collection in reverse but the solution you propose works fine as well.Tugboat
To clarify, you always need to be careful using loops to delete items out of an array because when you delete item [0], the item [1] moves to [0]. The loop then moves on to the next number as it does not realise the array has been rearranged. This solution takes a snapshot of the processes so when one is killed it is not deleted from the array the loop is working on.Orr
Something has changed since Windows 1909 version as I tried this. I cannot seem to fix it though.Lives
H
3
Stop-Process -Name explorer

Kill all related processes

Holmen answered 1/7, 2020 at 15:7 Comment(1)
feels a little brutal as my main window taskbar disappears for a bitVillagomez
P
1

As per @ste's comment, the accepted answer may have stopped working after windows updates.

I've solved it with something similar in Powershell.

Watchout though as I've assumed any explorer processes with a window title are folders. This might not always be the case (eg maybe if copying files)

get-process | ?{ $_.ProcessName -eq 'Explorer' } | ?{ "" -ne $_.MainWindowTitle } | Stop-Process
Posting answered 15/10, 2020 at 12:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.