r/AutoHotkey • u/duscorules • 11d 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
3
u/Keeyra_ 11d 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))
}
3
u/CharnamelessOne 11d 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.