Windows Service Control Manager (SCM) manages services that implement the Windows service interface. You can register and inspect a service with
sc.exe, but an arbitrary batch file or desktop executable is not automatically a valid service.Register a service executable
Run Command Prompt as Administrator and supply the full path to an executable designed to run as a Windows service:
shell_snippet
Shell · 1 line|
| sc.exe create MyService binPath= "C:\Services\MyService.exe" start= auto |
The space after
binPath= and start= is required by sc.exe. Quote paths containing spaces. start= auto configures automatic startup; use start= demand for manual startup.Start, inspect, and stop
shell_snippet
Shell · 4 lines|
| sc.exe start MyService |
| sc.exe query MyService |
| sc.exe qc MyService |
| sc.exe stop MyService |
query shows current status; qc shows the configured executable, startup type, and service account. If startup fails, inspect the service's logs and Windows Event Viewer.Remove the registration
shell_snippet
Shell · 2 lines|
| sc.exe stop MyService |
| sc.exe delete MyService |
Deletion removes the service registration, not the application files. A service may remain marked for deletion until open handles are closed.
What about batch files and ordinary programs?
Do not point
binPath directly at a .bat file or use cmd.exe /c start and assume SCM can control it. Use a service-aware wrapper or implement the Windows service interface, and configure its shutdown, working directory, account, and logging. For a task that only needs to run on a schedule or at sign-in, Task Scheduler may be a better fit.Service changes generally require administrator privileges. Test under the intended service account and grant it only the access it needs.
✦ 卷终 · 柯航存卷 ✦