Kill All Processes by Name

Sometimes, it happens that many AutoHotkey scripts remain in the background of the system and needs to close them. The following function would be handy for such task.

It can kill not only AutoHotkey scripts but also other applications by specifying the process name.

```ahk:KillAllAutoHotkeyScripts.ahk
F1::	; Press F1 to close all processes whose name is AutoHotkey.exe.
	Process, Exist 
	nThisPID := ErrorLevel	;the script PID
	TrayTip, KillAHKScripts, Closing AutoHotkey Scripts, 5, 1
	if (nKilled := KillAllProcessByName("AutoHotkey.exe", nThisPID)	)
		TrayTip, KillAHKScripts, % "Killed " nKilled " process(es).", 5, 1
	nKilled := ""
Return

KillAllProcessByName(strProcessName, strExceptPIDs="") {
	;[Parameters]
	;	strProcesName : the proess name to be closed. e.g. AutoHotkey.exe
	;	strExceptPIDs : a list of eception PIDs not to be closed delimited by commas. e.g. 1234,5678,987
	;[Return Value]
	;	the count of closed processes.
	nCount := 0
	Loop {
		nClosedPID := False
		for process in ComObjGet("winmgmts:").ExecQuery("Select Name,ProcessID from Win32_Process Where Name='" strProcessName "'") {
			nLoopPID := process.ProcessID
			if nLoopPID not in %strExceptPIDs%
			{
				Process, Close, % process.ProcessID
				if (nClosedPID := ErrorLevel)
					nCount++
			}
		}
	} Until !nClosedPID
	Return nCount
}
```

[![Alt Before](http://lh3.googleusercontent.com/-BNv0Nx0j6VM/Tzj9kf8hLOI/AAAAAAAAAXI/XDh8LLDm6pc/s288/KillAllProcessesByName01.jpg)](http://picasaweb.google.com/lh/photo/MZeZGpm_ZM1_ArKYSmvR2FC3SJHtXOU11nyieEYZgDY?feat=directlink)
[![Alt After](http://lh6.googleusercontent.com/-rnqKLUCAKVg/Tzj9kQn0_lI/AAAAAAAAAXM/yKVdwUx_6Vc/s288/KillAllProcessesByName02.jpg)](http://picasaweb.google.com/lh/photo/uoDpobKpo2DcMYOQaYEwUVC3SJHtXOU11nyieEYZgDY?feat=directlink)


This can be extended to leave the oldest instance but kill others. For example, multiple instances of explorer.exe also remain in the background when they are launched via Notepad++. In the following code, the function, FindOldestProcessByName(), finds the oldest process of the given name. Pressing F2 lets you close instances of explorer.exe except the one owning the task bar and the desktop.

```ahk:KillExplorers.ahk
F2::	; Press F2 to close all Explorer.exe but leave the oldest one.
	sExplorer := "Explorer.exe"
	TrayTip, Kill Explorers, Closing %sExplorer%, 5, 1
	if (nKilled := KillAllProcessByName(sExplorer, FindOldestProcessByName(sExplorer)))
		TrayTip, Kill Explorers, % "Killed " nKilled " process(es).", 5, 1
	nKilled := ""
Return

KillAllProcessByName(strProcessName, strExceptPIDs="") {
	;[Parameters]
	;	strProcesName : the proess name to be closed. e.g. AutoHotkey.exe
	;	strExceptPIDs : a list of eception PIDs not to be closed delimited by commas. e.g. 1234,5678,987
	;[Return Value]
	;	the count of closed processes.
	nCount := 0
	Loop {
		nClosedPID := False
		for process in ComObjGet("winmgmts:").ExecQuery("Select Name,ProcessID from Win32_Process Where Name='" strProcessName "'") {
			nLoopPID := process.ProcessID
			if nLoopPID not in %strExceptPIDs%
			{
				Process, Close, % process.ProcessID
				if (nClosedPID := ErrorLevel)
					nCount++
			}
		}
	} Until !nClosedPID
	Return nCount
}

FindOldestProcessByName(strProcessName) {
	For oProc in ComObjGet("winmgmts:").ExecQuery("Select Name,ProcessID,CreationDate from Win32_Process Where Name='" strProcessName "'") {
		if !nOldestPID {
			nOldestPID := oProc.ProcessID
			strOldestPIDCreationDate := oProc.CreationDate
		} else if (strOldestPIDCreationDate > oProc.CreationDate) {
			nOldestPID := oProc.CreationDate
			strOldestPIDCreationDate := oProc.CreationDate
		}			
	}
	return nOldestPID
} 
```

![Alt Result](https://lh6.googleusercontent.com/-sy1Uo7cqaMQ/TzkBpsK-GsI/AAAAAAAAAYE/NbQuGhTZOgI/s800/KillExplorers01.jpg)