Pregunta

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?

¿Fue útil?

Solución

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() }

Otros consejos

Stop-Process -Name explorer

Kill all related processes

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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top