Installing and Uninstalling Windows Services from the Command Line
Windows Services are background processes that run without direct user interaction. They are essential for many applications and system functions. While often managed through the Services application (services.msc), you can also install and uninstall them directly from the command line. This tutorial will guide you through the process.
Prerequisites
- Administrative Privileges: You must run the command prompt as an administrator. Right-click on the Command Prompt icon and select "Run as administrator".
- Service Executable: You need the executable file (.exe) for your Windows Service. This file contains the code that defines the service’s behavior.
- .NET Framework: The service needs to be built against a specific .NET framework version.
Using InstallUtil.exe
The primary tool for installing and uninstalling services from the command line is InstallUtil.exe
, a part of the .NET Framework. This tool handles the necessary registration with the Windows Service Control Manager.
1. Locating InstallUtil.exe:
The location of InstallUtil.exe
depends on the version of the .NET Framework installed on your system. Common paths include:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\
(for .NET Framework 4.0)C:\Windows\Microsoft.NET\Framework64\v4.0.30319\
(for 64-bit applications using .NET Framework 4.0)- Adjust the path according to the specific .NET Framework version your service is targeting.
2. Installing a Service:
Navigate to the directory containing InstallUtil.exe
using the cd
command in the command prompt. Then, use the following command to install the service:
InstallUtil.exe "path\to\your\service.exe"
Replace "path\to\your\service.exe"
with the actual path to your service’s executable file. If the path contains spaces, enclose it in double quotes.
Example:
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
InstallUtil.exe "C:\Services\MyService.exe"
If the installation is successful, you typically won’t see any explicit confirmation message. You can verify the service is installed by opening the Services application (services.msc) and checking the list.
3. Uninstalling a Service:
To uninstall a service, use the same InstallUtil.exe
command with the -u
flag.
InstallUtil.exe -u "path\to\your\service.exe"
Example:
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
InstallUtil.exe -u "C:\Services\MyService.exe"
Like installation, a successful uninstallation usually doesn’t produce an explicit confirmation message. Confirm the service is removed by checking the Services application.
Using SC CREATE
Another method to install Windows services from the command line involves using the SC CREATE
command. This command provides more control over service parameters.
Syntax:
SC CREATE "ServiceName" binpath= "PathToExecutable"
ServiceName
: The name you want to give the service.binpath
: The full path to the service executable.
Example:
SC CREATE "MySvc" binpath= "D:\Me\Services\MySvc\MySvc.exe"
This command creates a service named "MySvc" and sets the executable path to the specified location. You can configure other parameters like startup type and dependencies with additional arguments to the SC CREATE
command.
Batch Script Example
For convenience, you can create a batch script (.bat) to automate the installation or uninstallation process. Here’s an example:
@echo off
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
InstallUtil.exe "C:\Services\MyService.exe"
if ERRORLEVEL 1 goto error
exit
:error
echo There was a problem installing the service.
pause
Save this code as a .bat
file (e.g., install_service.bat
) and run it as an administrator. The script changes the directory to the .NET Framework directory and then executes InstallUtil.exe
to install the service. It also includes error handling.
Important Considerations
- Dependencies: If your service relies on other services, you may need to configure dependencies using
SC CREATE
or other tools. - Permissions: Ensure that the account running the service has the necessary permissions to access resources.
- Error Handling: Always include error handling in your scripts to catch potential problems during installation or uninstallation.
- 64-bit vs. 32-bit: Ensure that you are using the correct version of
InstallUtil.exe
(32-bit or 64-bit) for your application.