Step-by-Step Guide to Manually Create and Install a Windows Service

In Windows, services operate in the background, allowing applications to run without user interaction and start automatically during boot. This guide walks you through the process of creating a new system service from an executable file using built-in tools available in Windows.

To create a service, you can utilize the sc.exe command from the command prompt or the New-Service cmdlet in PowerShell. Here’s an example command using sc.exe:

sc create CorpCollectorService binPath= "C:Program Files (x86)CORPcollector.exe -i C:toolsconfig.xml" start= auto DisplayName= "CORP Log Collector Service"

Upon successful execution, you will receive a message indicating service creation success:

[SC] CreateService SUCCESS

Command Argument Breakdown:

  • CorpCollectorService: Name of the new service.
  • binPath: Full path to the executable file, including any required launch arguments (make sure to enclose paths with spaces in quotation marks).
  • start= auto: Configures the service to start automatically with Windows.
  • DisplayName: The name displayed in the services management console.

Additional parameters include type for the kind of service and obj to specify the user context under which the service runs.

To manage or view the service, you can open the Services management console by running services.msc and refreshing the list with F5. Service configurations are stored under the HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServices registry key.

Here’s how you can create a service with PowerShell:

New-Service -Name CorpCollectorService -BinaryPathName "C:Program Files (x86)CORPcollector.exe -i C:toolsconfig.xml" -DisplayName "my test service" -Description "CORP Log Collector Service" -StartupType "Automatic"

If the service needs specific user credentials, you would use:

$username = "user123"$password = "PaSSw0rd1"$securepassword = ConvertTo-SecureString $password -AsPlainText -Force$cred = New-Object System.Management.Automation.PSCredential ($username, $securepassword)New-Service … -Credential $cred

To properly remove a service, the command is:

sc delete service_name

It is essential to note that not all executables can function as Windows services. Applications must be designed to respond to commands from the Service Control Manager (SCM). If an application is not service-compatible, you may encounter an error such as:

Windows could not start the MyService1 service on Local Computer.Error 1053: The service did not respond to the start or control request in a timely fashion.

For applications that are not natively designed to run as services, consider using tools like the Non-Sucking Service Manager (NSSM). This modern tool allows you to run any executable as a service and offers better monitoring and management features compared to older utilities.

You can install NSSM via WinGet:

winget install --id NSSM.NSSM -e

To create a service using NSSM, run:

nssm install testservice "C:Toolscollector.exe"

NSSM also provides a user-friendly GUI for managing service properties and configurations, enhancing user experience when working with services on Windows.


Posted

in

,

by

Tags: