In command line, we can use Tasklist to find the process the service is running.
Tasklist /FI "SERVICES eq serviceA"
Use Taskkill to kill the process the service is running.
Taskkill /F /FI "SERVICES eq serviceA"
We can use sc command to query the service and delete the service.
sc query serviceA
If the service doesn't exists, it will show the following error:
[SC] EnumQueryServicesStatus:OpenService FAILED 1060:
The specified service does not exist as an installed service.
sc start serviceA
sc stop serviceA
sc delete serviceA
Or we can use net start|stop serviceA to start, stop a service.
In Window Batch script, we can use SC QUERY to query a service, and check ERRORLEVEL to determine whether a service exist:
SC QUERY serviceA > NUL
REM if ERRORLEVEL 1060 means it doesn't exists
IF ERRORLEVEL 1060 (
echo service doesn't exist
)
Manage Service in PowerShell
get-service serviceA
start-service serviceA
stop-service serviceA
restart-service serviceA
(Get-WmiObject Win32_Service -filter "name=''").Delete()
$svc = gwmi win32_service -filter "name='alerter'"
$svc.delete()
Resources
SC Command
Tasklist /FI "SERVICES eq serviceA"
Use Taskkill to kill the process the service is running.
Taskkill /F /FI "SERVICES eq serviceA"
We can use sc command to query the service and delete the service.
sc query serviceA
If the service doesn't exists, it will show the following error:
[SC] EnumQueryServicesStatus:OpenService FAILED 1060:
The specified service does not exist as an installed service.
sc start serviceA
sc stop serviceA
sc delete serviceA
Or we can use net start|stop serviceA to start, stop a service.
In Window Batch script, we can use SC QUERY to query a service, and check ERRORLEVEL to determine whether a service exist:
SC QUERY serviceA > NUL
REM if ERRORLEVEL 1060 means it doesn't exists
IF ERRORLEVEL 1060 (
echo service doesn't exist
)
Manage Service in PowerShell
get-service serviceA
start-service serviceA
stop-service serviceA
restart-service serviceA
(Get-WmiObject Win32_Service -filter "name='
$svc = gwmi win32_service -filter "name='alerter'"
$svc.delete()
Resources
SC Command