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.

Share:
14,923

Related videos on Youtube

Bita
Author by

Bita

Updated on September 18, 2022

Comments

  • Bita
    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 using start .ps1 either.

    • user364455
      user364455 over 6 years
      powershell -command "(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()}; Get-Process | Where-Object {$_.MainWindowTitle -ne \"\"} | stop-process"
    • root
      root over 6 years
      It sounds like you want to type this manually each time. You could reduce this Powershell by using aliases. For example, Where-Object becomes ?.
    • Bita
      Bita over 6 years
      Oh, it's so easy, thank you so much! That's right what I wanted :)
    • tvdo
      tvdo over 6 years
      Note 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.