Installing and Uninstalling Windows Service using Command Line
In this article we will create windows service Project from scratch and then install and uninstall it using the command Line so let’s get started
Create New Project Select Windows Service .Net from Visual studio
Once proejct has been Created you can have this project structure in your visual studio
Add Installer by Right Clicking on somewhere in the page like this
Then give your your service some name to get Install right Click on ServiceInstaller 1 and then go to Properties
then gave the Service name you like i am giving it the name WindowsService
Change Name and ServiceName
Change Start Type to Automatic
Go To ServiceProcessInstaller1 Right Click go to Properties and change Account Type to local Service
Save All and then Rebuild the Solution
Right Click and go to Open folder in Explorer
Go to Project Directory Open Bin and then Debug Folder
Create two file named Install and Uninstall.bat
Open Install.bat and write this code in it
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
@echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ):getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for 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"
"%temp%\OEgetPrivileges.vbs"
exit /B:gotPrivileges
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .cd /d %~dp0
%windir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil /i "WindowsService.exe"
pause
Open uninstall.bat and copy this code in it
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
@echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ):getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for 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"
"%temp%\OEgetPrivileges.vbs"
exit /B:gotPrivileges
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .cd /d %~dp0
%windir%\Microsoft.NET\Framework\v4.0.30319\InstallUtil /u "WindowsService.exe"
pause
once you have completed these steps make sure to open install.bat it will gave this file adminstrator right automatically to get install
Now the Service has been Installed to Uninstalled it simply open uninstall.bat file
Open Services.msc and Double check the service will be Remove