Install Uninstall Windows Service Using Batch File

Kailash Chandra Behera | Thursday, March 29, 2018

Introduction

The following blog provides code shippet for batch program to install and uninstall windows service using batch script in batch file.

Getting Started

In my previous blog, we discussed how to install or uninstall windows service using command prompt. Now, will discuss the batch file code which helps to install windows service.

As Like command prompt batch file also usages installutil.exe to install or uninstall windows service, following below are the path of installation. Exe for different versions of. Net Framework.

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

  1. Make sure you have added Service Installer to your service application .
  2. 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.
  3. Click the background of the designer to select the service itself, rather than any of its contents.
  4. 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.
  5. 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.
  6. To determine how your service will be started, click the ServiceInstaller component and set the StartType(Manual, Automatic, Disabled) property to the appropriate value.
  7. Click the ServiceProcessInstaller component and set the appropriate property values.
Installing Service

Syntax:

 @echo off  
 cd full path of installutil.exe "full path of your service "  
 if ERRORLEVEL 1 goto error  
 exit  
 :error  
 echo There was a problem  
 pause  

Example:

This following example create windows service with .NET Framework4.0 for 64 bit, hence it usage installutil from path C:\Windows\Microsoft.NET\Framework64\v4.0.30319. See the below example for more details.
 @echo off  
 cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe "D:\Myservices\myservice.exe "  
 if ERRORLEVEL 1 goto error  
 exit  
 :error  
 echo There was a problem  
 pause  
Uninstall Service
 @echo off  
 cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe -u "D:\Myservices\myservice.exe "  
 if ERRORLEVEL 1 goto error  
 exit  
 :error  
 echo There was a problem  
 pause  

Here in the above we was how to create windows installer service by batch program, I hope you have enjoyed it a lot.

Thanks