How to Install and configure a Windows Service from the command line

By FoxLearn 3/19/2025 7:52:40 AM   25
This article explains how to install, configure, query, and uninstall a Windows Service using command line utilities installutil.exe and sc.exe.

1. Add a Service Installer Class to Your Windows Service Project

To install your Windows Service using installutil.exe, you'll first need to add a service installer class to your project.

If you skip this step, you'll encounter the following error:

Error: No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\Services\TestService.exe assembly.

Steps to Create a Service Installer Class:

  1. Open your service class file in design mode in Visual Studio.
  2. Right-click the design surface and choose Add Installer.
  3. A new installer class will be generated and opened in design mode.

At a minimum, you'll need to set the ServiceName and Account properties for the installer class.

  1. Set the ServiceName Property:

    • Click on serviceInstaller1 to bring up its properties.
    • Specify the ServiceName for your service.
  2. Set the Account Property:

    • This defines the account under which the service will run. Choose an appropriate account based on your needs (consult a security specialist if unsure).
    • Click on serviceProcessInstaller1 and adjust the Account setting as required.

After adding the installer class, Visual Studio will automatically open ProjectInstaller.cs in design mode, allowing you to configure these settings.

2. Install and Configure the Service

Now that your service is ready for installation, you can use the following batch script to install it via installutil.exe, configure it with sc.exe, and start the service with net start.

The script also logs the output with a timestamp.

For example:

@ECHO OFF

REM Get log file name with timestamp
for /f %%a in ('wmic os get LocalDateTime ^| findstr ^[0-9]') do (set ts=%%a)
set LogName="C:\logs\installTestService%ts:~0,8%%ts:~8,4%%ts:~12,2%.log"

REM Install service
set servicePath="C:\Services\TestService.exe" 
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" %servicePath% >> %LogName%

REM Add dependency on SQL Server
sc config TestService depend=MSSQL$SQLEXPRESS >> %LogName%

REM Set to start automatically
sc config TestService start= auto >> %LogName%

REM Configure service to restart after 1 minute on failure
sc failure TestService actions= restart/60000/restart/60000// reset= 86400 >> %LogName%

REM Start the service
net start TestService >> %LogName%

In this example:

  • This script installs TestService.exe using installutil.exe.
  • The service is configured to depend on SQL Server Express (MSSQL$SQLEXPRESS), ensuring it won’t run unless SQL Server Express is running.
  • The service is set to start automatically and restart within 1 minute if it crashes. After two failures, it will stop attempting to restart until the failure counter is reset after 1 day.
  • All output is logged to a file located in C:\logs.

Note: Run this script as Administrator to ensure proper installation.

3. Query the Service Properties and Status

You can check the service's properties and status either from the Services application or by running a command with sc.exe in the command line.

To Query Service Properties:

Use the following command to get the configuration of the service:

sc qc TestService

The output will show the current configuration of the service, similar to the example below:

[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: TestService
        TYPE               : 10  WIN32_OWN_PROCESS
        START_TYPE         : 2   AUTO_START
        ERROR_CONTROL      : 1   NORMAL
        BINARY_PATH_NAME   : "C:\Services\TestService.exe"
        DISPLAY_NAME       : TestService
        DEPENDENCIES       : MSSQL$SQLEXPRESS
        SERVICE_START_NAME : NT AUTHORITY\LocalService

To Query the Failure Configuration:

Use the following command to check the failure actions:

sc qfailure TestService

This will display the failure configuration set during installation:

[SC] QueryServiceConfig2 SUCCESS

SERVICE_NAME: TestService
        RESET_PERIOD (in seconds)    : 86400
        FAILURE_ACTIONS              : RESTART -- Delay = 60000 milliseconds.
                                       RESTART -- Delay = 60000 milliseconds.

4. Uninstall the Service

If you need to uninstall the service, use the following batch script. It will uninstall the service and log the results to a timestamped log file.

Batch File Example to Uninstall:

@ECHO OFF

REM Get log file name with timestamp
for /f %%a in ('wmic os get LocalDateTime ^| findstr ^[0-9]') do (set ts=%%a)
set LogName="C:\logs\uninstallTestService%ts:~0,8%%ts:~8,4%%ts:~12,2%.log"

REM Uninstall service
set servicePath="C:\Services\TestService.exe" 
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" %servicePath% -u >> %LogName%

Running this script will uninstall the service and log the results in a file (e.g., C:\logs\uninstallTestService20210205074023.log).

To check if the service has been successfully uninstalled, run the following command:

sc query TestService

If the service is uninstalled, you’ll see the error:

[SC] EnumQueryServicesStatus:OpenService FAILED 1060:
The specified service does not exist as an installed service.