Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing All Explorer Windows in PowerShell

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?

like image 511
Wayne Z Avatar asked Jul 19 '13 19:07

Wayne Z


1 Answers

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() }
like image 163
CB. Avatar answered Sep 19 '22 12:09

CB.