How to install windows service with command prompt

/, command prompt, Tips & Tricks/How to install windows service with command prompt

To install a windows service using the command line you can use the installutil.exe located at the path where Windows .NET Framework is installed.

If you are like me, you don’t want always to keep typing and check if the user you are logged in as to the server or the box generally has the right UAC access etc. You can always “automate” the whole process by simply creating a batch file which accepts two¬†arguments to install/uninstall the service and the path to the executable of the windows service.

Below is the the batch file I’m using

:: Check & get admin rights
@echo off
ECHO =============================
ECHO Check for Privileges
ECHO =============================

if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO **************************************
ECHO Set Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
exit /B 

setlocal & pushd .

cd /d %~dp0

:Change this line to point to the correct .NET Framework
%windir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil %1 %2

In the last line before pause where InstallUtil command is set, “%windir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil” change
To use it simply create a batch file with the above code and pass as a firtargument /i to install the windows service or /u to uninstall and as a second argument the path to your windows service executable.

Example for installing a windows service:

MyWindowsServiceBatch.bat /i "C:\PathToExecutable\MyWindowsService.exe"

Example for uninstalling a windows service:

MyWindowsServiceBatch.bat /u "C:\PathToExecutable\MyWindowsService.exe"
2017-03-07T20:40:10+00:00 December 13th, 2016|.net, command prompt, Tips & Tricks|0 Comments

Leave A Comment