TechBuzzPro

How to Create a Windows Service: Step-by-Step Guide

· 6 min read · Windows & PC

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Creating a Windows service is not something you do through a single built-in menu; you write a small program that implements the service contract and then register it with Windows using a tool such as sc.exe or InstallUtil.exe. The service then runs in the background under a dedicated account, starting automatically at boot if you configure it that way. This guide walks through the practical route using .NET and the built-in Service Control Manager tools, and explains what to check when the service refuses to start.

What You'll Need

  • Windows 10 or Windows 11 (the steps also work on Windows Server).
  • Administrator rights on the machine — registering a service requires elevation.
  • A development environment: Visual Studio with the .NET desktop development workload, or the .NET SDK if you prefer the command line.
  • Basic familiarity with C# or another .NET language. If you plan to write the service in another language, you will need that language's own service-hosting library; the registration steps below still apply.

Step-by-Step

  1. Create the project. In Visual Studio, choose Create a new project and select the Worker Service template (or Windows Service if your installed SDK offers it). Name the project, pick a folder, and target a current .NET version. The Worker Service template gives you a BackgroundService class with an ExecuteAsync method — that method is where your long-running work goes. If you use the older Windows Service template, you get a ServiceBase-derived class with OnStart and OnStop overrides instead.
  2. Add the service hosting package. For a Worker Service, open the NuGet package manager and install Microsoft.Extensions.Hosting.WindowsServices. In Program.cs, chain .UseWindowsService() onto the host builder. This makes the same executable run as a console app during development and as a proper service when registered.
  3. Write the work. Put your logic inside ExecuteAsync (or OnStart). Keep startup fast: do not block for long inside OnStart, because the Service Control Manager expects the service to report started within a short timeout. If the work is long-running, start a background task and return. Log to a file or the Windows Event Log so you can see what happened after the service starts without a console window.
  4. Build and publish. Build the project in Release mode. For a self-contained deployment that does not depend on a separately installed runtime, publish with the runtime identifier for your machine (for example win-x64). Note the full path to the resulting .exe — you will need it in the next step.
  5. Open an elevated command prompt. Right-click Command Prompt or Terminal and choose Run as administrator. Registration will fail without elevation.
  6. Register the service. Use the built-in Service Control Manager tool: sc.exe create "MyServiceName" binPath= "C:\full\path\to\YourApp.exe" start= auto. Note the spaces after the equals signs — sc.exe requires them. Use start= demand if you want the service to start only when asked. The display name defaults to the service name; you can set a friendlier one with DisplayName=.
  7. Set the account. By default the service runs as LocalSystem, which has broad privileges. For most applications, a dedicated low-privilege account or the built-in Network Service account is safer. Change it in the Services console (services.msc) under the service's Log On tab, or with sc.exe config.
  8. Start and verify. Run sc.exe start "MyServiceName", or open services.msc, find the entry, and click Start. Check the service status with sc.exe query "MyServiceName". If it reports RUNNING, confirm your application actually did its job — check its log file or the Event Viewer under Windows Logs > Application.
  9. Test a restart. Stop the service, start it again, and reboot the machine to confirm it comes up automatically if you set start= auto. This catches path and permission problems that only appear outside your development session.
  10. Remove it when done. Stop the service first, then run sc.exe delete "MyServiceName". Deleting a running service leaves it in a marked-for-deletion state until reboot.

Common Mistakes

  • Missing spaces in sc.exe arguments. binPath= and start= must be followed by a space before the value. Without it, the command fails with a cryptic error.
  • Registering a Debug build path. The bin folder can be cleaned or moved. Point the service at a stable published location.
  • Forgetting UseWindowsService(). Without it, the executable may exit immediately when started by the Service Control Manager because it does not implement the service contract.
  • Assuming the working directory. A service starts in C:\Windows\System32, not your app folder. Use absolute paths or resolve paths relative to the executable.
  • Running as LocalSystem unnecessarily. This gives the service far more access than most tasks need and widens the impact of any bug.
  • Blocking in OnStart. Long initialization causes the Service Control Manager to time out and report the service as failed even though your code is still running.

Tips

  • Log early and often. A service has no console, so file or Event Log output is your only window into what it is doing.
  • Keep the service name short and without spaces; the display name can be descriptive.
  • Use the Recovery tab in the service's properties to configure automatic restart on failure.
  • Test the published build, not the debug build, before registering — runtime and path behaviour can differ.
  • If you need to debug, run the same executable as a console app first; the Worker Service host supports that natively.

Troubleshooting

If the service fails to start, work from simplest to most involved. First run sc.exe query "MyServiceName" to see the reported state, then open Event Viewer and check Windows Logs > Application for errors from your service or from the Service Control Manager. Error 1053 (the service did not respond in a timely fashion) usually means startup blocked too long or the service contract was not implemented — check that UseWindowsService() is present. Error 5 (access denied) means the account lacks rights to the executable or its folders; verify the path and permissions. Error 2 (file not found) almost always means the binPath is wrong — quote paths that contain spaces. If the service starts but does nothing, confirm it is actually the published build and that your logging is writing somewhere you can read. If it starts and then stops immediately, look for an unhandled exception in the Event Log and run the executable from a console to see the output directly. As a last resort, delete the service with sc.exe delete, reboot if it is stuck, and register it again with a corrected path.

Frequently Asked Questions

Can I create a Windows service without writing code?

Not in the sense of turning an arbitrary program into a service. Windows services must implement the service contract. You can use a wrapper tool that hosts an existing executable as a service, but the wrapped program still needs to behave well when it has no interactive desktop.

Do I need Visual Studio?

No. The .NET SDK and a text editor are enough to build and publish a Worker Service from the command line. Visual Studio just makes project creation and debugging more convenient.

What is the difference between sc.exe create and InstallUtil?

sc.exe is a built-in Windows tool that registers any service executable. InstallUtil runs installer components embedded in a .NET assembly. Both produce a registered service; sc.exe requires no extra tooling and is the simpler choice for most projects.

Why does my service work in Visual Studio but not after registration?

The two environments differ in working directory, user account, and whether a console is attached. Use absolute paths, check the account's permissions, and make sure logging is configured so you can see startup errors.

Can I run a service under my own user account?

Yes, you can set the logon account in the service properties, but the account needs the Log on as a service right. A dedicated service account is preferable to a personal one.

How do I update a service I already registered?

Stop the service, replace the published files at the registered path, and start it again. You do not need to re-register unless the path or service name changes.