r/AutoHotkey • • 5d ago

v2 Script Help Close all CMD windows ?

How can i create a script that will close all open CMD (command prompt) windows, using the shortcut CTRL + ALT + W ?

4 Upvotes

5 comments sorted by

3

u/CharnamelessOne 5d ago

The Beginner tutorial will tell you how to make a hotkey.

WinGetList will get you an array of the handles of all the cmd windows, given an identifier called WinTitle.

You can get that identifier via WindowSpy, AHK's built-in tool. Using the process name, the class name or both is the most sensible option, most of the time.

A For loop can be used to perform an action on every item of the array returned by WinGetList.

That action should be WinClose.

1

u/Lawson_Herring58 1d ago

WinGetList plus WinClose is a nice clean way to handle this without resorting to the taskkill flamethrower

3

u/Keeyra_ 5d ago

May be a bit too much for a noobie, to consider the edge cases like old windows cmd.exe vs. terminal. WinClose with ProcessClose fallback, so:

#Requires AutoHotkey 2.0
#SingleInstance

DetectHiddenWindows(1)

^!w:: {
    for target in ["ahk_exe cmd.exe", "ahk_exe WindowsTerminal.exe"]
        for hwnd in WinGetList(target)
            !WinClose(hwnd, 1) && ProcessClose(WinGetPID(hwnd))
}

2

u/faz712 5d ago

taskkill /f /im wt.exe /im cmd.exe

😂

1

u/bceen13 5d ago

Instead of using a shortcut, use the WinClose command/function. The key is WinTitle. Every window has an ahk_exe identifier.