Get command lines, including parameters, for all running processes

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Get command lines, including parameters, for all running processes

Post by JoeWinograd » 04 Dec 2022, 19:26

Hi Folks,

The Windows Task Manager shows the command line, including parameters, that was used to run a process. For example:

Task Manager shows command line.png
Task Manager shows command line.png (19.31 KiB) Viewed 1038 times

Is there a way with AHK code to get the command lines, including parameters, of all running processes? This must include processes that do not have an existing window. The processes DO have a tray icon, but the reason for this question is that the excellent TrayIcon functions (original author Sean, update author Cyruz, most recent author @FanaticGuru) do not work with the latest release of Windows 11 (I'll be posting separately on this soon, but right now I need an immediate work-around). I've been using the Tooltip entry returned by the TrayIcon_GetInfo function to determine the multiple instances of my program that are running, but with that not working in W11, I need another way, and getting my program's process name along with its parameters will do the trick.

For anyone curious about the TrayIcon functions not working in W11, run this simple code:

Code: Select all

TrayInfo:=TrayIcon_GetInfo()
MsgBox % TrayInfo.MaxIndex()
#Include <TrayIconLibrary.ahk>
Works fine in W10 (and earlier), showing the number of icons in the tray, but the result is null in W11 (at least, in my release of it, which is Windows 11 Home, Version 22H2, Build 22623.1020). Thanks very much, Joe

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Get command lines, including parameters, for all running processes

Post by mikeyww » 04 Dec 2022, 19:33

WMIC can get you there. viewtopic.php?p=380787#p380787https://www.cyberithub.com/20-useful-wm ... ic_command

Code: Select all

out := A_ScriptDir "\procs.txt", Clipboard := ""
RunWait, %ComSpec% /c wmic process get CommandLine | clip,, Hide
ClipWait, 0
If !ErrorLevel {
 FileRecycle, %out%
 FileAppend, % Clipboard := RegExReplace(Clipboard, "\h*\R\s*", "`n"), %out%
 Run, %out%
} Else MsgBox, 48, Error, An error occurred while waiting for the clipboard.

User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Get command lines, including parameters, for all running processes

Post by JoeWinograd » 04 Dec 2022, 20:25

Hi Mikey,

Thanks for the wmic idea! This will provide a nice work-around for me:

Code: Select all

wmic process where "name='ProcessName.exe'" get CommandLine
I can parse everything coming back from that (via StdOutToVar) to determine which instances of my program are running.

I hope that @FanaticGuru can get the TrayIcon library working on the latest W11 (it worked on previous W11 builds). I use it for much more than TrayIcon_GetInfo, including TrayIcon_Button, TrayIcon_Move, and TrayIcon_Remove. Regards, Joe

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Get command lines, including parameters, for all running processes

Post by flyingDman » 04 Dec 2022, 23:27

It seems wmic is deprecated (https://learn.microsoft.com/en-us/windows/win32/wmisdk/wmic)
try something like this:

Code: Select all

cnt := 0
gui, add, listview, w1000 r40,#|ID|name|commandline
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process")
	lv_add("",++cnt,process.processID,process.name,process.commandline)
LV_ModifyCol()
gui, show
14.3 & 1.3.7

User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Get command lines, including parameters, for all running processes

Post by JoeWinograd » 05 Dec 2022, 02:50

Hi @flyingDman,
Thanks for that code...works perfectly...very nice! Got a surprise when I ran it...noticed a gazillion AutoHotkeyU64.exe processes (from tests that I ran while troubleshooting an issue). The interesting thing is that the processes do not appear in the Process tab of Task Manager, which is where I always look to kill processes during testing...they appear in the Details tab, where I never look. :) Good to know! Regards, Joe

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Get command lines, including parameters, for all running processes

Post by jNizM » 05 Dec 2022, 09:37

JoeWinograd wrote:
05 Dec 2022, 02:50
The interesting thing is that the processes do not appear in the Process tab of Task Manager, which is where I always look to kill processes during testing...they appear in the Details tab, where I never look.
For this I recommend always "Process Explorer" from Sysinternals
https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile


User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Get command lines, including parameters, for all running processes

Post by jNizM » 05 Dec 2022, 10:01

Btw they renamed "Process Hacker" to "System Informer"
https://systeminformer.sourceforge.io/
https://github.com/winsiderss/systeminformer
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

User avatar
JoeWinograd
Posts: 2179
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Get command lines, including parameters, for all running processes

Post by JoeWinograd » 05 Dec 2022, 10:56

Hi jNizM and Mikey,
Thanks for the ideas...much appreciated! Regards, Joe

Post Reply

Return to “Ask for Help (v1)”