Reputation: 1046
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?
Upvotes: 17
Views: 30793
Reputation: 181
It's much easier to just do taskkill /f /fi "SERVICES eq <service_short_name>"
Upvotes: 18
Reputation: 57282
@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
Upvotes: 3
Reputation: 394
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! )
Upvotes: 19