Generally windows services are deployed one of two ways.  One way is in an installation package, that will handle the installation for a user.  The service may be one of several parts of an installation package, and the package approach makes the installation easier to maintain.  This method is mostly useful for an unknown number of installation points (i.e. machines), and mandatory when the installation is for an end user on their personal device.

The second way which I focus on here, is by using the InstallUtil.exe utility in the .NET Framework.  This console application has a few handy uses:

  • bulk deployment of a single, simple service across a number of servers in a given cluster or data center.
  • scripting the install/uninstall as an option into the Windows Explorer context menu for executable files.

I’ve rediscovered two files I have been using for a while, which can facilitate this specific interface to the context menu in Windows Explorer.  The first is a command file which can be executed directly, but is designed to be called from a Windows explorer context menu. It currently supports version 2.0 or 4.0 of the utility, but can be easily adjusted as new versions of InstallUtil debut in later frameworks.

The second is the registry file to merge into the HKEY_CLASSES_ROOT to create the Windows Explorer context menu for the install/uninstall options. Simply create a folder C:\Scripts\ExplorerContext, copy these two files into it, merge the registry settings file, and then you’ll see four new options on an executable file for installing or uninstalling the service in .NET version 2.0 or 4.0

The process to install these files is to place these files in a folder, and merge the registry file.  Be sure to adjust the paths in the Registry file content to the correct path to the command file, if it is note the default:  C:\Script\ExplorerContent

To use the file from Explorer Context, copy the service executable and support files to their operating folder.  From Windows Explorer, right click on the entry executable for the service, and select the Service Install menu option for the version on .NET needed.

Likewise, to uninstall the service, select one of the uninstall options.

That’s it.  The command file is below.  You can download both the command file and the registry file as a zip file here.  This is a very useful was of automating installs for bulk code updates.

@echo off

REM install_services_dotNET.cmd -- use the dotNET InstallUtil.exe console app to install/uninstall
REM a windows service.
REM J. Schultz -- 3/11/2010

REM Interface to Windows explorer as a context menu item set using the following registry settings
REM NOTE: Adjust the path to the cmd file as needed (C:\\Scripts\\ExplorerContext\\).

REM ------------------------------------- 8< --------------------------------------
REM Windows Registry Editor Version 5.00
REM 
REM [HKEY_CLASSES_ROOT\exefile\shell]
REM 
REM [HKEY_CLASSES_ROOT\exefile\shell\.NET 2.0 Service Install]
REM 
REM [HKEY_CLASSES_ROOT\exefile\shell\.NET 2.0 Service Install\command]
REM @="C:\\Scripts\\ExplorerContext\\install_services_dotNET.cmd I 2.0 \"%1\""
REM
REM [HKEY_CLASSES_ROOT\exefile\shell\.NET 2.0 Service Uninstall]
REM 
REM [HKEY_CLASSES_ROOT\exefile\shell\.NET 2.0 Service Uninstall\command]
REM @="C:\\Scripts\\ExplorerContext\\install_services_dotNET.cmd U 2.0 \"%1\""
REM 
REM [HKEY_CLASSES_ROOT\exefile\shell\.NET 4.0 Service Install]
REM 
REM [HKEY_CLASSES_ROOT\exefile\shell\.NET 4.0 Service Install\command]
REM @="C:\\Scripts\\ExplorerContext\\install_services_dotNET.cmd I 4.0 \"%1\""
REM 
REM [HKEY_CLASSES_ROOT\exefile\shell\.NET 4.0 Service Uninstall]
REM 
REM [HKEY_CLASSES_ROOT\exefile\shell\.NET 4.0 Service Uninstall\command]
REM 
REM @="C:\\Scripts\\ExplorerContext\\install_services_dotNET.cmd U 4.0 \"%1\""
REM
REM ------------------------------------- 8< --------------------------------------
 
if /I "%1" == "I" goto CONT01 
if /I "%1" == "U" goto CONT01 
echo First parameter must I for install, or U for uninstall. 
goto HELP 
 
:CONT01 
if "%2" NEQ "" if /I "%2" == "2.0" goto CONT02 
if "%2" NEQ "" if /I "%2" == "4.0" goto CONT02 
echo Second parameter must specify the dotNET version: 2.0 or 4.0 
goto HELP 
 
:CONT02 
if "%3" NEQ "" goto CONT03 
echo Third parameter must specify the full path to the executable file for the service.
goto HELP 
 
:CONT03 
if exist %3 goto CONT04 
echo The executable file for the service (third parameter) specifies a non-existent file. 
goto HELP 
 
:CONT04 
if "%4" EQU "" goto CONT05 
echo Expected three parameters, but have four or more. 
goto HELP 
 
:CONT05 
REM Parameter check passed 
REM %~d3 
REM cd "%~p3" 
 
if /I "%1" == "I" echo This will attempt to ** INSTALL ** the file... 
if /I "%1" == "U" echo This will attempt to ** REMOVE ** the file... 
echo. 
echo %3 
echo. 
echo .. as a service.  Press ENTER to continue, or Ctrl+Break to abort. pause > nul
echo.
 
if /I "%1" == "I" if /I "%2" == "2.0" "%windir%\microsoft.net\framework\v2.0.50727\installutil.exe" %3
 
if /I "%1" == "U" if /I "%2" == "2.0" "%windir%\microsoft.net\framework\v2.0.50727\installutil.exe" /u %3
 
if /I "%1" == "I" if /I "%2" == "4.0" "%windir%\Microsoft.NET\Framework\v4.0.30319\installutil.exe" %3
 
if /I "%1" == "U" if /I "%2" == "4.0" "%windir%\Microsoft.NET\Framework\v4.0.30319\installutil.exe" /u %3
 
echo.
goto END
 
:HELP
echo.
echo Example command lines:
echo.
echo    install_services_dotNET.cmd I 2.0 "C:\Program Files\My Company\My Service\ServiceApp.exe"
echo    install_services_dotNET.cmd U 2.0 "C:\Program Files\My Company\My Service\ServiceApp.exe"
echo.
echo    install_services_dotNET.cmd I 2.0 "C:\Program Files\My Company\My Service\ServiceApp.exe"
echo    install_services_dotNET.cmd U 2.0 "C:\Program Files\My Company\My Service\ServiceApp.exe"
echo.
 
:END
echo Press any key to close this window.
pause > nul

The two files below demonstrate what is needed for windows context.  One is a batch file