1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Kill All Processes by Name

Last updated at Posted at 2012-02-13

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.

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
Alt After

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.

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

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?