Closing All Explorer Windows

by Oct 28, 2008

Sometimes, explorer windows clutter up the desktop. You can use a COM library called Shell.Application to automatically close all open explorer windows and clean up your windows world programmatically. Use the following line:

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

Essentially, this line instantiates the COM object, uses its Windows() method to retrieve all ShellWindow windows (including IE and Outlook), then filters the windows so that only true explorer windows are left, and finally quits all explorer windows.

Hungry for more? Check out this blog entry!