Dan
Posts: 4530
Joined: Wed Apr 24, 2013 3:51 pm

Install .NET Windows service under LocalSystem or a custom user account

Hello,

Note!
This sample uses a Windows service written in C# and targeting .NET Framework 4.8.
The same overall approach can be used for testing and installing other Windows services as well, even if their implementation differs.

I am attaching a sample project that shows how to install and test a Windows service in the following scenarios:
  • running under the LocalSystem account
  • running under a specified user account

The sample package also includes an Advanced Installer project, together with all required resources, so it can be built and tested directly.

Service logging behavior
The sample service writes entries to a log file when the service:
  • starts
  • stops
This makes it easy to confirm that the service was started successfully and to verify under which account it is running. The log folder is created automatically when the service writes its first entry, so no manual creation is required.

Log file location
The service writes the log file under the Windows Common Application Data location.

In this sample, the log file path is:

Code: Select all

%ProgramData%\Caphyon\ServiceSample\ServiceHeartbeat.log

So, once the service is started or stopped, the log file should be created there automatically.

The output can look like this:

Code: Select all

2026-04-21 14:38:09 - Service started under Local System account     
2026-04-21 14:39:30 - Service stopped under '<ComputerName>\<LogonUser>' account
This helps verify:
  • the service start/stop lifecycle
  • the account used to run the service


Manual testing with SC.exe
Before configuring any service installation through the installer, make sure the service can be installed, started and stopped outside the installer.

Running these command will ensure the service installation under the LocalSystem account:

Code: Select all

sc.exe create ADemoServiceByCaphyonAsSystem binPath="<service_path>"
sc.exe start ADemoServiceByCaphyonAsSystem
sc.exe service properties.png
sc.exe service properties.png (89.63 KiB) Viewed 32543 times

For configuring the service to run under a specified account, run this command:
sc.exe create ADemoServiceByCaphyonAsUser binPath="<service_path>" obj=".\<YourUsername>" password="TheAccountPassword"
sc.exe start ADemoServiceByCaphyonAsUser

Important note for custom user accounts
If the service is installed under a custom user account, that account must have the Windows user right:
  • Log on as a service

This can be verified in:
  • Local Security Policy
  • Security Settings > Local Policies > User Rights Assignment > Log on as a service
If that right is missing, the service will fail to start under the selected account.

service-policy.png
service-policy.png (350.68 KiB) Viewed 32543 times


Notes!
LocalSystem typically does not require manual assignment of the Log on as a service right.


Installer-based service configuration

Once the service was tested and everything is working as expected, we can configure the installer to handle service installation!


1) Add resources in the Files and Folders view

files-and-folders.png
files-and-folders.png (23.43 KiB) Viewed 32543 times


2.1 Create service that runs under the LogonUser

To configure the service that runs under specified account, make sure you specify the user and passowrd under the Account section.
service-configuration.png
service-configuration.png (82.9 KiB) Viewed 32543 times


2.2 Create service that runs under LocalSystem account

service-configuration-localsystem.png
service-configuration-localsystem.png (81.61 KiB) Viewed 32543 times

Feel free to download the sample project attached.
Service installation.zip
(8.94 KiB) Downloaded 612 times

All the best,
Advanced Installer Team
Dan Ghiorghita - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Sample Projects”