Process

Performs one of the following operations on a process: checks if it exists; changes its priority; closes it; waits for it to close.

Process, SubCommand , PIDOrName, Value

Parameters

SubCommand, Value
These are dependent upon each other and their usage is described below.
PIDOrName

A number (the PID) or process name as described below. This parameter can be blank or omitted, depending on the sub-command being used.

PID: The Process ID, which is a number that uniquely identifies one specific process (this number is valid only during the lifetime of that process). The PID of a newly launched process can be determined via the Run command. Similarly, the PID of a window can be determined with WinGet. The Process command itself can also be used to discover a PID.

Name: The name of a process is usually the same as its executable (without path), e.g. notepad.exe or winword.exe. Since a name might match multiple running processes, only the first process will be operated upon. The name is not case-sensitive.

Sub-commands

For SubCommand, specify one of the following:

Exist

Checks whether the specified process is present.

Process, Exist , PIDOrName

ErrorLevel is set to the Process ID (PID) if a matching process exists, otherwise 0. If PIDOrName is blank or omitted, the script's own PID is retrieved. An alternate, single-line method to retrieve the script's PID is PID := DllCall("GetCurrentProcessId").

Close

Forces the first matching process to close.

Process, Close, PIDOrName

If a matching process is successfully terminated, ErrorLevel is set to its former Process ID (PID). Otherwise (i.e. there was no matching process or there was a problem terminating it), it is set to 0. Since the process will be abruptly terminated -- possibly interrupting its work at a critical point or resulting in the loss of unsaved data in its windows (if it has any) -- this sub-command should be used only if a process cannot be closed by using WinClose on one of its windows.

To force all matching processes to close, see example #6.

List

Although this sub-command is not yet supported, example #4 demonstrates how to retrieve a list of processes via DllCall().

Priority

Changes the priority level of the first matching process.

Process, Priority, PIDOrName, Level

Changes the priority (as seen in Windows Task Manager) of the first matching process to Level and sets ErrorLevel to its Process ID (PID). If PIDOrName is blank, the script's own priority is changed. If there is no matching process or there was a problem changing its priority, ErrorLevel is set to 0.

Level should be one of the following letters or words: L (or Low), B (or BelowNormal), N (or Normal), A (or AboveNormal), H (or High), R (or Realtime). Note: Any process not designed to run at Realtime priority might reduce system stability if set to that level.

Wait

Waits for the specified process to exist.

Process, Wait, PIDOrName , Timeout

Specify for Timeout the number of seconds (can contain a decimal point) to wait before timing out. If Timeout is blank or omitted, the sub-command will wait indefinitely. If a matching process is discovered, ErrorLevel is set to its Process ID (PID). If the sub-command times out, ErrorLevel is set to 0.

Processes are checked every 100 milliseconds; the moment the condition is satisfied, the sub-command stops waiting. In other words, rather than waiting for the timeout to expire, it immediately sets ErrorLevel as described above, then continues execution of the script. Also, while the sub-command is in a waiting state, new threads can be launched via hotkey, custom menu item, or timer.

WaitClose

Waits for all matching processes to close.

Process, WaitClose, PIDOrName , Timeout

Specify for Timeout the number of seconds (can contain a decimal point) to wait before timing out. If Timeout is blank or omitted, the sub-command will wait indefinitely. If all matching processes are closed, ErrorLevel is set to 0. If the sub-command times out, ErrorLevel is set to the Process ID (PID) of the first matching process that still exists.

Processes are checked every 100 milliseconds; the moment the condition is satisfied, the sub-command stops waiting. In other words, rather than waiting for the timeout to expire, it immediately sets ErrorLevel as described above, then continues execution of the script. Also, while the sub-command is in a waiting state, new threads can be launched via hotkey, custom menu item, or timer.

ErrorLevel

ErrorLevel is set to 0 if a sub-command failed or timed out. Otherwise, it is set to a Process ID (PID). See the sub-commands above for details.

Run, WinGet, WinClose, WinKill, WinWait, WinWaitClose, WinExist()

Examples

Launches Notepad, sets its priority to high and reports its current PID.

Run notepad.exe,,, NewPID
Process, Priority, %NewPID%, High
MsgBox The newly launched Notepad's PID is %NewPID%.

Waits for a Notepad process to appear. If one appears within 5.5 seconds, its priority is set to low and the script's own priority is set to high. After that, an attempt is made to close the process within 5 seconds.

Process, Wait, notepad.exe, 5.5
NewPID := ErrorLevel  ; Save the value immediately since ErrorLevel is often changed.
if not NewPID
{
    MsgBox The specified process did not appear within 5.5 seconds.
    return
}
; Otherwise:
MsgBox A matching process has appeared (Process ID is %NewPID%).
Process, Priority, %NewPID%, Low
Process, Priority,, High  ; Have the script set itself to high priority.
WinClose Untitled - Notepad
Process, WaitClose, %NewPID%, 5
if ErrorLevel ; The PID still exists.
    MsgBox The process did not close within 5 seconds.

Press a hotkey to change the priority of the active window's process.

#z:: ; Win+Z hotkey
WinGet, active_pid, PID, A
WinGetTitle, active_title, A
Gui, 5:Add, Text,, Press ESCAPE to cancel, or double-click a new`npriority level for the following window:`n%active_title%
Gui, 5:Add, ListBox, vMyListBox gMyListBox r5, Normal|High|Low|BelowNormal|AboveNormal
Gui, 5:Add, Button, default, OK
Gui, 5:Show,, Set Priority
return

5GuiEscape:
5GuiClose:
Gui, Destroy
return

MyListBox:
if (A_GuiEvent != "DoubleClick")
    return
; else fall through to the next label:
5ButtonOK:
GuiControlGet, MyListBox
Gui, Destroy
Process, Priority, %active_pid%, %MyListBox%
if ErrorLevel
    MsgBox Success: Its priority was changed to "%MyListBox%".
else
    MsgBox Error: Its priority could not be changed to "%MyListBox%".
return

Shows a list of running processes retrieved via DllCall().

d := "  |  "  ; string separator
s := 4096  ; size of buffers and arrays (4 KB)

Process, Exist  ; Sets ErrorLevel to the PID of this running script.
; Get the handle of this script with PROCESS_QUERY_INFORMATION (0x0400):
h := DllCall("OpenProcess", "UInt", 0x0400, "Int", false, "UInt", ErrorLevel, "Ptr")
; Open an adjustable access token with this process (TOKEN_ADJUST_PRIVILEGES = 32):
DllCall("Advapi32.dll\OpenProcessToken", "Ptr", h, "UInt", 32, "PtrP", t)
VarSetCapacity(ti, 16, 0)  ; structure of privileges
NumPut(1, ti, 0, "UInt")  ; one entry in the privileges array...
; Retrieve the locally unique identifier of the debug privilege:
DllCall("Advapi32.dll\LookupPrivilegeValue", "Ptr", 0, "Str", "SeDebugPrivilege", "Int64P", luid)
NumPut(luid, ti, 4, "Int64")
NumPut(2, ti, 12, "UInt")  ; Enable this privilege: SE_PRIVILEGE_ENABLED = 2
; Update the privileges of this process with the new access token:
r := DllCall("Advapi32.dll\AdjustTokenPrivileges", "Ptr", t, "Int", false, "Ptr", &ti, "UInt", 0, "Ptr", 0, "Ptr", 0)
DllCall("CloseHandle", "Ptr", t)  ; Close the access token handle to save memory.
DllCall("CloseHandle", "Ptr", h)  ; Close the process handle to save memory.

hModule := DllCall("LoadLibrary", "Str", "Psapi.dll")  ; Increase performance by preloading the library.
s := VarSetCapacity(a, s)  ; An array that receives the list of process identifiers:
c := 0  ; counter for process idendifiers
DllCall("Psapi.dll\EnumProcesses", "Ptr", &a, "UInt", s, "UIntP", r)
Loop, % r // 4  ; Parse array for identifiers as DWORDs (32 bits):
{
    id := NumGet(a, A_Index * 4, "UInt")
   ; Open process with: PROCESS_VM_READ (0x0010) | PROCESS_QUERY_INFORMATION (0x0400)
    h := DllCall("OpenProcess", "UInt", 0x0010 | 0x0400, "Int", false, "UInt", id, "Ptr")
    if !h
        continue
    VarSetCapacity(n, s, 0)  ; A buffer that receives the base name of the module:
    e := DllCall("Psapi.dll\GetModuleBaseName", "Ptr", h, "Ptr", 0, "Str", n, "UInt", A_IsUnicode ? s//2 : s)
    if !e    ; Fall-back method for 64-bit processes when in 32-bit mode:
        if e := DllCall("Psapi.dll\GetProcessImageFileName", "Ptr", h, "Str", n, "UInt", A_IsUnicode ? s//2 : s)
            SplitPath n, n
    DllCall("CloseHandle", "Ptr", h)  ; Close the process handle to save memory.
    if (n && e)  ; If image is not null add to list:
        l .= n . d, c++
}
DllCall("FreeLibrary", "Ptr", hModule)  ; Unload the library to free memory.
;Sort, l, C  ; Uncomment this line to sort the list alphabetically.
MsgBox, 0, %c% Processes, %l%

Shows a list of running processes retrieved via COM and Win32_Process.

Gui, Add, ListView, x2 y0 w400 h500, Process Name|Command Line
for proc in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
    LV_Add("", proc.Name, proc.CommandLine)
Gui, Show,, Process List

Forces all matching processes to close.

ProcessCloseAll(PIDOrName)
{
    Loop {
        Process, Close, % PIDOrName
        Process, Exist, % PIDOrName  ; Improves reliability in some cases.
    } Until not ErrorLevel
}

; Example:
Loop 3
    Run, notepad.exe
Sleep 3000
ProcessCloseAll("notepad.exe")