Get the PID of a Windows service by the name of the service

26.9k views Asked by At

Is there a way of getting the PID of a windows service with a command in a batch script by just knowing the name of the service?

3

There are 3 answers

4
Inquisitive On BEST ANSWER

Try the following code:

FOR /F "tokens=3" %%A IN ('sc queryex %serviceName% ^| findstr PID') DO (SET pid=%%A)
 IF "!pid!" NEQ "0" (
  taskkill /F /PID !pid!
 )
0
npocmaka On
@echo off

for /f "tokens= delims=" %%# in ('
  wmic service where "name='Service'" get ProcessId /format:value
') do (
  for /f "tokens=* delims=" %%$ in ("%%#") do set "%%$"
)

taskkill /pid %ProcessId% /f
0
Matthew Carlson On

It's much easier to just do taskkill /f /fi "SERVICES eq <service_short_name>"