Install and configure a Windows Service from the command line

In this article I’ll show how to install and configure a Windows Service, query its status and properties, and then how to uninstall it. I’ll be using the command line utilities installutil.exe and sc.exe to install and configure the service.

Add a service installer class to your Windows Service project

In order to use installutil.exe to install your Windows Service, you need to add a service installer class. Otherwise when you try to install, you’ll get the following error:

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

To create a service installer class:

  • Open your service class file in design mode in Visual Studio.
  • Right-click > click Add Installer.
Service component design mode, right-click and click Add Installer

After you click Add Installer, it will create the service installer class and open it in design mode.

At a bare minimum, you need to set the ServiceName and Account properties.

  • Click serviceInstaller1 to bring up its properties.
  • Specify the ServiceName property.
After adding the installer, the ProjectInstaller.cs component will pop up in design mode. Click on serviceInstaller1 and fill in the ServiceName property

Next, set the Account property. This property is the account that your service runs as, so choose whatever makes sense in your case (if you’re not sure, ask a security person).

  • Click serviceProcessInstaller1 to bring up its properties.
  • Set the Account to whatever value is appropriate in your situation.
In ProjectInstaller.cs, click serviceProcessInstaller and set the appropriate service account

Install the service and configure it

The following batch file installs TestService.exe by using installutil.exe, configures the service with sc.exe, and starts the service using net start:

@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 Make start automatically
sc config TestService start= auto >> %LogName%

REM On crash, restart after 1 minute
sc failure TestService actions= restart/60000/restart/60000// reset= 86400 >> %LogName%

REM Start the service
net start TestService >> %LogName%
Code language: plaintext (plaintext)

Running this installs the service and outputs to a timestamped log file (ex: C:\logs\installTestService20210205074016.log).

Note: Execute this by using Run as Administrator.

It configures the service with the following properties:

  • Depends on service MSSQL$SQLEXPRESS (SQL Server Express). This means it won’t run unless SQL Server Express is running. When SQL Server Express restarts, it also restarts TestService.
  • Starts automatically.
  • Restarts after 1 minute if the service crashes. Note: This is only configuring it to auto-restart twice. After two failures, it won’t attempt to auto-restart. It resets the failure counter after 1 day.

Query the service properties and status

You can look at the service properties and status either by looking in Services, or by querying the properties from the command line by using sc.exe.

Execute the following to get the service properties and status:

sc qc TestServiceCode language: plaintext (plaintext)

This shows that the service is running and it’s using the configuration specified in the batch install script (from the previous section):

[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"
        LOAD_ORDER_GROUP   :
        TAG                : 0
        DISPLAY_NAME       : TestService
        DEPENDENCIES       : MSSQL$SQLEXPRESS
        SERVICE_START_NAME : NT AUTHORITY\LocalService
Code language: plaintext (plaintext)

Execute the following to check the failure configuration:

sc qfailure TestServiceCode language: plaintext (plaintext)

This shows that it’s using the failure configuration specified in the batch install script:

[SC] QueryServiceConfig2 SUCCESS

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

Uninstall the service

The following batch file uninstalls the service and outputs the results to a timestamped log:

@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 Install service
set servicePath="C:\Services\TestService.exe" 
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" %servicePath% -u >> %LogName%
Code language: plaintext (plaintext)

Running this uninstalls the service and outputs to a timestamped log file (ex: C:\logs\uninstallTestService20210205074023.log).

You can check if the service is uninstalled by checking if sc query returns an error, like this:

C:\WINDOWS\system32>sc query TestService
[SC] EnumQueryServicesStatus:OpenService FAILED 1060:

The specified service does not exist as an installed service.Code language: plaintext (plaintext)