How to Create (Install) a New Windows Service in Batch Script
Windows Services are background processes that can start automatically when the computer boots, even before a user logs in. For developers building server-side agents, monitoring tools, or long-running applications, converting a standard executable into a Windows Service is a standard requirement.
This guide will explain how to use the sc create command in a Batch script to install and configure a new Windows Service from any executable file.
The SC CREATE Command
The sc (Service Control) utility is the built-in tool for managing the Service Control Manager database. The create sub-command allows you to register a new service by providing a name and a path to the file.
Basic Creation Syntax
sc create "MyServiceName" binPath= "C:\path\to\your\app.exe"
The Space Mystery.
In the sc command, there must be a space after the equals sign in parameters like binPath= and start= . Writing binPath="C:\..." (without the space) is the most common cause of the command failing with a "Syntax Error."
Creating a Service with Configuration
A simple creation is usually not enough; you often want to set the "Friendly Name" (what the user sees), a description, and the start type (Manual vs. Automatic).
Script Example: Full Service Installation
@echo off
set "SvcName=MyCustomAgent"
set "AppPath=C:\MyApps\Agent.exe"
:: Verify the executable exists
if not exist "%AppPath%" (
echo [ERROR] Executable not found: %AppPath%
pause
exit /b 1
)
:: Check if the service already exists
sc query "%SvcName%" >nul 2>&1
if %errorlevel% equ 0 (
echo [INFO] Service '%SvcName%' already exists. Skipping creation.
pause
exit /b 0
)
echo [ACTION] Installing service: %SvcName%...
:: 1. Create the service
:: start= auto means it starts when the computer boots
sc create "%SvcName%" binPath= "%AppPath%" start= auto DisplayName= "My Custom Background Agent"
if %errorlevel% neq 0 (
echo [ERROR] Failed to create service. Ensure you are running as Administrator.
pause
exit /b 1
)
:: 2. Add a description (Optional)
sc description "%SvcName%" "This service monitors the local system for changes."
:: 3. Try to start the service immediately after creation
echo [ACTION] Starting service...
net start "%SvcName%"
if %errorlevel% equ 0 (
echo [SUCCESS] Service '%SvcName%' installed and started.
) else (
echo [WARNING] Service was created but failed to start.
echo The executable may not be a valid Windows Service application.
echo Check the Windows Event Viewer for details.
)
pause
Breakdown of Parameters:
binPath=: The path to the executable file. Always wrap this in quotes if the path has spaces.start= auto: Sets the service to start automatically on boot. Other options includemanualanddisabled.DisplayName=: The human-readable name shown in the Services manager (services.msc).
Important Considerations for Service Apps
Not every program can be a Windows Service. A standard console app or a GUI app (like Notepad.exe) will usually fail to start as a service because they don't respond to the "Service Start" signals from Windows.
- Compatibility: To run as a service, the executable must be written to use the Service API (e.g., using
ServiceBasein .NET or similar hooks in C++). - Non-Service Wrapper: If you need to run a standard
.bator.exeas a service, you should use a wrapper utility like NSSM (the Non-Sucking Service Manager) or WinSW.
Best Practices and Rules
1. Administrative Privileges
Creating a service is a major system change. Your Batch script must be run as an Administrator. If you aren't an admin, sc create will return "Access Denied."
2. Quoting the binPath
If your path contains spaces, you must use a specific quoting format for the binPath to ensure it is stored correctly in the registry.
Correct Way:
sc create "MySvc" binPath= "\"C:\Program Files\App\my.exe\""
The \" escape sequence tells the system to include the quotes inside the path string.
3. Absolute Paths Only
Never use relative paths (like ..\my.exe) for the binPath. Windows needs the absolute location of the file so it can find it during the boot process before the user environment is loaded.
How to Avoid Common Errors
Wrong Way: Forgetting the space after "="
Wrong Approach:
:: This will result in a syntax error
sc create "MySvc" binPath="C:\app.exe"
Correct Way:
sc create "MySvc" binPath= "C:\app.exe"
Best Practice: Setting Service Dependencies
If your service needs a database or network connection to be ready first, you can define dependencies:
:: This ensures your service starts AFTER the Network Connections service
sc create "MySvc" binPath= "C:\app.exe" depend= Netman
Real-World Use Case: Auto-Installing a Custom Agent
Imagine you have a monitoring tool you want to deploy to several servers.
@echo off
title Service Auto-Installer
set "SvcName=MyAgent"
set "AppPath=%~dp0agent.exe"
:: Verify the executable exists
if not exist "%AppPath%" (
echo [ERROR] Agent executable not found: %AppPath%
pause
exit /b 1
)
:: Check if already exists
sc query "%SvcName%" >nul 2>&1
if %errorlevel% equ 0 (
echo [INFO] Service '%SvcName%' already exists. Skipping installation.
pause
exit /b 0
)
echo [ACTION] Registering new system service...
sc create "%SvcName%" binPath= "%AppPath%" start= auto obj= "NT AUTHORITY\LocalService"
if %errorlevel% neq 0 (
echo [ERROR] Failed to create service. Ensure you are running as Administrator.
pause
exit /b 1
)
sc description "%SvcName%" "Company-wide data collection agent."
echo [SUCCESS] Service '%SvcName%' installed.
echo Start it with: net start "%SvcName%"
pause
Conclusions
Creating a Windows Service in Batch script remains the fastest way to register background processes. While sc create is powerful, its strict syntax regarding spaces and absolute paths requires careful attention. By following these rules and ensuring your script runs with elevated privileges, you can easily integrate your custom applications into the Windows lifecycle.