This article will walk you through the steps to install and uninstall a Windows Service using the Command Prompt (cmd.exe
).
Getting Started
Windows Services are long-running executable applications that run in the background. They can be automatically started when the computer boots, run without user interaction, and be paused or stopped. If you're a developer or IT professional, managing services using the Command Prompt can be faster and more script-friendly than using graphical tools.
Installing Windows service is as simple as installing software, but is bit difference from software installation.
InstallUtil.exe utility is required to install Windows Service and you will find this utility from below path if installed Visual Studio. This utility is varying for a different version of the .NET framework of Visual Studio and System, you need to select correct utility based on your framework version in which you have built your Windows Service.
Microsoft provides variety of InstallUtil.exe which based on .NET FRAMEWORK, following below are the path of InstallUtil.exe
for different version.
Utility Paths: For 32-bit Machine
- .Net Framework 4 and above: C:\Windows\Microsoft.NET\Framework\v4.0.30319
- .Net Framework 3.5: C:\Windows\Microsoft.NET\Framework\v3.5
- .Net Framework 3.0: C:\Windows\Microsoft.NET\Framework\v3.0
- .Net Framework 2.0: C:\Windows\Microsoft.NET\Framework\v2.0.50727
- .Net Framework 1.0: C:\Windows\Microsoft.NET\Framework\v1.0.3705
- .Net Framework 1.1: C:\Windows\Microsoft.NET\Framework\v1.1.4322
Utility Paths: For 64-bit Machine
- .Net Framework 4 and above: C:\Windows\Microsoft.NET\Framework64\v4.0.30319
- .Net Framework 3.5: C:\Windows\Microsoft.NET\Framework64\v3.5
- .Net Framework 3.0: C:\Windows\Microsoft.NET\Framework64\v3.0
- .Net Framework 2.0: C:\Windows\Microsoft.NET\Framework64\v2.0.50727
- .Net Framework 1.0: C:\Windows\Microsoft.NET\Framework64\v1.0.3705
- .Net Framework 1.1: C:\Windows\Microsoft.NET\Framework64\v1.1.4322
Precaution
- Make sure you have added Service Installer to your service application .
- If you not added go to Solution Explorer. In Solution Explorer, access Design view for the service for which you want to add an installation component.
- Click the background of the designer to select the service itself, rather than any of its contents.
- With the designer in focus, right-click, and then click Add Installer. A new class, ProjectInstaller, and two installation components, ServiceProcessInstaller and ServiceInstaller, are added to your project, and property values for the service are copied to the components.
- Click the ServiceInstaller component and verify that the value of the ServiceName property is set to the same value as the ServiceName property on the service itself.
- To determine how your service will be started, click the ServiceInstaller component and set the StartType(Manual, Automatic, Disabled) property to the appropriate value.
- Click the ServiceProcessInstaller component and set the appropriate property values.
If you have installed Visual Studio in your local machine or server, you can install Windows Service through Developer Command Prompt or you can install using system command Prompt. Go through the below steps to install Windows Service.
Installing Service
- Build your service application with release mode and take the builds int to a path, this will be path of your installed windows service.
- Open Developer Command Prompt execute the InstallUtil.exe with command
- The syntax of command is:
C:\>"Full path of installutil.exe" "Full path of service.exe"
- Here I will install windows service with .NET Framework version 4.0, hence command for my service will be
C:\>"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" "Full path of service.exe"
- Use the above command and press Enter
Uninstalling Service
Uninstalling service syntax is same as installation, but little difference like below.
C:\>C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe -u "C:\Services\myservice.exe"
Summary
Using sc.exe and InstallUtil.exe, you can easily install and uninstall Windows Services via the command line. This is especially useful for automation, deployment scripts, or working in server environments where GUIs are limited. I hope this was helpful to you.
Thanks