I am using tasklist to bring me information about a specific service/proccess running on my Windows Server.
The command:
tasklist /svc /fi "SERVICES eq .Service02"
The output:
Image Name PID Services
================== ======== ============================================
app02.exe 15668 .Service02
I searched for quite a while now here on StackOverflow, other forums and also on Windows Docs but I couldn't figure out how to get the desired output, which is:
15668
I managed to do a command that kind of worked but not really...
for /f "tokens=1,2 delims= " %A in ('tasklist /svc /fi "SERVICES eq .Service02"') do echo %B
This did not give me the desired output - Instead, it gave me the following output:
C:\Users\admin>echo Name
Name
C:\Users\admin>echo ========
========
C:\Users\admin>echo 15668
15668
If I could only do something that only echoed the third line. The output would be exactly what I need. The PID.
So, I need a command that brings the name of the proccess being used by the service I provide, and return me only its PID.
Please, can someone help me?
Edit: Thanks to @Squashman I managed to do a new command:
tasklist /svc /fi "SERVICES eq .Service02" /FO csv /NH
"service02.exe","15668",".Service02"
And now the output is:
"service02.exe","15668",".Service02"
But where do I go from here?
Just use a
for /Floop to capture the CSV output of thetasklistcommand and to extract the right token.In Command Prompt:
In a batch file:
The
~-modifier removes the surrounding quotation marks from the PID value.