I need to be able to get the PID from a running process (cmd.exe) using the command line. The problem is there are two cmd.exe running. One is under the username SYSTEM and one is compUser. Is there a way that I can grab the PID of the compUser cmd.exe?
Edit: This needs further explanation. I'm doing this from a batch file. One of the calls that I'm making in my batch file starts a cmd.exe that never dies. So killing that cmd.exe would be simple:
taskkill /F /IM cmd.exe /FI "username eq compUser"
The problem is that the batch file that I'm in is being handled by another instance of cmd.exe under the username compUser. What I'm attempting to do is get the PID from the original cmd.exe before I start the second cmd.exe. That way I can just use the command:
taskkill /F /IM cmd.exe /FI "username eq compUser" /FI "PID neq [orignal task's PID]"
I'd like just to share my piece of code, maybe it will be useful for somebody. It is based on the comment from jiggawagga, but it can terminate many processes:
right before I start server process.
I set the Title to my own cmd window, other cmd windows what I call or run by "start /b" I rename to other window title name. Next I detect PID, set MYPID and kill all other cmd with OTHER PID of my.
All on one file, working in other label loop with timeout dalay.
The way I ended up having to do this was use:
right before I started the batch script that hangs. Then when I was ready to kill the hanging cmd window:
There is probably a better way, but this worked.
This will display all processes named "cmd.exe" for the user "compUser":