在PowerShell中关闭所有资源管理器Windows

Way*_*e Z 4 powershell explorer powershell-2.0

我正在编写以下代码以使用PowerShell关闭所有资源管理器窗口:

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

但它没有关闭所有打开的窗户.相反,它只关闭RoundDown(N/2)+1窗口,并RoundUp(N/2)-1打开窗户.

有人能帮忙吗?

CB.*_*CB. 12

我认为管道中出现了一些问题.此代码有效:

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

 $a | % {  $_.Quit() }
Run Code Online (Sandbox Code Playgroud)

  • 在处理其内容时修改集合的经典问题.通过杀死explorer.exe实例,Windows正在更新集合,同时您还在行走它.这通常会导致不良.:-)一个常见的工作是反向走集合,但你提出的解决方案也可以正常工作. (5认同)