Install and Uninstall Windows Service using batch scripts

In this article, you can learn how to Install and Uninstall Windows Service using command line batch scripts (.bat), making your task easy.


The windows service applications created using .NET Framework use the command line utility InstallUtil.exe to install and uninstall the services. This tool is usually available under the Microsoft .NET framework installation path. It can vary the version number.

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe

How to: Install and uninstall Windows services using command line:

To install the .NET service, you need to run the below command by using command-prompt by specifying the full path of the .NET service application.


InstallUtil “D:\MyApp\MyService.exe”

To Uninstall the .NET service, you need to run the below command by using command-prompt by specifying the full path of the .NET service application.


InstallUtil /u “D:\MyApp\MyService.exe”

How to: Install and Uninstall Windows Service using batch scripts:

However, Installing and uninstalling Windows services using the command line is usually performed manually. This involves opening the windows command prompt, navigating to the installutil.exe path, and entering the command. So, this looks costly task. Below are the two batch scripts (.bat) that perform the install and uninstall windows service with a single-click run to minimize the effort.

You need to save these scripts into two different files with the .bat extension and run the scripts whenever required.

Note: Always make sure that run the batch scripts (.bat) in Administrator mode in order to install/uninstall windows service.

How to Install Windows service batch script?

@ECHO OFF

REM The following directory is for .NET 4.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

echo Installing MyService.exe Win Service.
echo -------------------------------
InstallUtil "D:\MyApp\MyService.exe"
echo -------------------------------
pause
echo Done.

UnInstall Windows service batch script:

@ECHO OFF

REM The following directory is for .NET 4.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX2%

echo Un-Installing MyService.exe Win Service.
echo -------------------------------
InstallUtil  /u "D:\MyApp\MyService.exe"
echo -------------------------------
pause
echo Done.

Note: The v4.0.30319 is the framework version.


– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Recommended:   How to log Windows Service custom logs in C#
Share this:
We will be happy to hear your thoughts

Leave a reply

Troubleshootyourself
Logo