Close all opened Windows by ONE CMD Command
14,923
Solved by PetSerAl.
powershell -command "(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()}; Get-Process | Where-Object {$_.MainWindowTitle -ne \"\"} | stop-process"
Note that Stop-Process
will actually end the entire process.
Related videos on Youtube
Author by
Bita
Updated on September 18, 2022Comments
-
Bita over 1 year
I would like to close all opened windows (from programs, windows explorer, etc...) by using CMD. The easiest way I found is not using CMD but running these two powershell commands:
(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()} Get-Process | Where-Object {$_.MainWindowTitle -ne ""} | stop-process
Which works pretty well, but I don't know how to execute them right from CMD. I tried the commands below using
powershell -noexit
to execute powershell commands and^
to ignore some cmd functions, but it does not work:powershell -noexit "(New-Object -comObject Shell.Application^).Windows(^) ^| foreach-object {$_.quit(^)}" powershell -noexit "Get-Process ^| Where-Object {$_.MainWindowTitle -ne ""} ^| stop-process"
And I do not want to use
taskkill
command or create a .ps1 file and execute it by usingstart .ps1
either.-
user364455 over 6 years
powershell -command "(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()}; Get-Process | Where-Object {$_.MainWindowTitle -ne \"\"} | stop-process"
-
root over 6 yearsIt sounds like you want to type this manually each time. You could reduce this Powershell by using aliases. For example,
Where-Object
becomes?
. -
Bita over 6 yearsOh, it's so easy, thank you so much! That's right what I wanted :)
-
tvdo over 6 yearsNote that
Stop-Process
will actually end the entire process, which can include multiple windows and any background component. Also this is somewhat dangerous in that it can leave any open files in a bad state. You're better off gracefully closing the windows where possible.
-