When developing a Windows service, you spend some time on install/uninstall cycles. To manage this, you use the command line tool “sc.exe”.
Get Service Information
Use PowerShell to get the current running/stopped status.
PS> get-service sqlbrowser Status Name DisplayName ------ ---- ----------- Running sqlbrowser SQL Server Browser
You can use wildcards to get related service names.
PS> get-service sql* Status Name DisplayName ------ ---- ----------- Running SQLBrowser SQL Server Browser Stopped SQLSERVERAGENT SQL Server Agent (MSSQLSERVER) Running SQLTELEMETRY SQL Server CEIP service (MSSQLSERVER) Running SQLWriter SQL Server VSS Writer
Start/Stop Service
PowerShell provides commands to easily start and stop services.
PS> start-service MyService PS> stop-service MyService
Install Service
You can install the service using the “sc.exe” command. It is a bit finicky though. You MUST include a space between the command switch and the value. For instance, we specify the path to the service with “binpath=” and then you MUST include a space between that and the path. The path must be the absolute location of the service file.
Also, you typically run a service as “LocalService”. The LocalService account is a predefined account used by the service control manager. It has minimum privileges on the local computer and presents anonymous credentials on the network.
sc.exe create MyService binpath= "C:\dev\MyService\MyService.dll" obj= "NT AUTHORITY\LocalService"
If the creation fails due to “[SC] CreateService FAILED 1072: The specified service has been marked for deletion” then you need to make sure you have exited all the MMC.exe snap-ins dealing with tasks (Task Manager) and services (Service Manager). Close all instances of these and retry the creation.
Remove Service
You can use “sc.exe” to remove services as well.
sc.exe delete MyService
Remove Service Manually
If the service does not uninstall cleanly (or at all), you can do it manually using regedit.exe.
- Start a command line in admin mode.
- Run “regedit.exe”.
- Navigate to “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services” key.
- Right-click the service and select “Delete”.
- You will be asked “Are you sure you want to permanently delete this key and all of its subkeys.”. Click “Yes”.
Now if in your case, your service still appears in the Task Manager Service tab, you can do one final thing to remove what is left.
- Start a command line in admin mode.
- Change directory to “c:/windows/system32”
- Run “sfc /scannow” (the System File Checker application)
- The app scan will begin and you will be directed to review a log.
- Restart the computer and the service should no longer appear in your Task Manager.
And if that doesn’t do it, you need to reboot!
Additional Help: The specified service has been marked for deletion
If result is “The specified service has been marked for deletion” then make sure you have closed the “Services.msc” window or service may not disappear. For additional help with problems removing the service, see How to solve “The specified service has been marked for deletion” error.