Install a Windows service using a Windows command prompt?
Asked Answered
C

21

406

I want to install a Windows service using a Windows command prompt (not the Visual Studio command prompt).

How do I do this?

Calvaria answered 17/11, 2011 at 9:32 Comment(0)
R
596

Navigate to the installutil.exe in your .net folder (for .net 4 it's C:\Windows\Microsoft.NET\Framework\v4.0.30319 for example) and use it to install your service, like this:

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" "c:\myservice.exe"

Regarding a comment, for 64bit apps, use below:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe
Ryurik answered 17/11, 2011 at 9:52 Comment(9)
If it is the x64 compiled service, use "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\installutil.exe".Crabber
I get a weird error. No public installer with the RunInstallerAttribute. Yes attribute could be found in the .exe assembly. Remove InstallState file because there are no installersDogcatcher
Hi, I tried this solution but its giving exception as "...file not found or one of its dependencies." Please help.Homo
For everyone with the same problem as all these and me: You dont need to put the whole path in it anymore. a simple installutil.exe "C:\YourProject.exe" is enough.Ellsworth
Is there any difference between installutil in 'Framework' and 'Framework64'?Flake
@Ellsworth You do have to use the full path, unless you added it to the path environment variable.Concinnate
Similarly to uninstall we can use "/u" as additional flag, for an example: "C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" /u "c:\myservice.exe"Ballplayer
FANTASTIC! The most simple is always the best! Wasted time with installers, this command line did it in a snap! Only trouble I had is I could NOT HAVE it set to start after install, so I removed that and it worked. Thx!Elevenses
Thank you so much for this!! I was wracking my brain on where to the utility was located. Microsofts documentation just assumed it's in the path.Volkslied
L
394

Nothing wrong with SC Create command. Just you need to know the correct args :

SC CREATE "MySVC" binpath= "D:\Me\Services\MySVC\MySVC.exe"
Lyricism answered 30/10, 2017 at 1:26 Comment(8)
This is the only answer that references the official way to install/modify/delete a service. Moreover, this doesn't depends on the .NET Framework being installed and works with any kind of file, not just .NET binaries.Rogation
Yep..thats why i loved this approach and always advice it since all the work needs to be done into the exe which can belong to any .net fw version.Lyricism
why does it give me ` A positional parameter cannot be found that accepts argument 'binpath='. `Smedley
Note that the space after "binpath= " is imperative. @ParamvirSinghKarwalRenee
Is there any downside to this vs using InstallUtil?Alphonsa
SC CREATE is handy as you can specify a different service name than might be configured in the program. Handy when you want to install the same program as different services. See here #1705054Melodic
If using powershell to run this command note that SC is an alias for Set-Content. Writing SC.exe will fix that.Demimonde
Not only is this the ONLY correct answer, but the comment about it only being available to Windows Server is completely incorrect. I hope that @CaptainDashenka will remove that comment in order not to mislead anyone. It is available, at least under Windows 10 Professional, but you must enter sc.exe as sc is the service control manager, which is another thing entirely. Documentation is here: learn.microsoft.com/en-us/windows-server/administration/…Pol
T
89

If the directory's name has a space like c:\program files\abc 123, then you must use double quotes around the path.

installutil.exe "c:\program files\abc 123\myservice.exe"

Install windows service from command prompt

It makes things much easier if you set up a bat file like following,

e.g. To install a service, create a "myserviceinstaller.bat" and "Run as Administrator"

@echo off
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
installutil.exe "C:\Services\myservice.exe"

if ERRORLEVEL 1 goto error
exit
:error
echo There was a problem
pause

to uninstall service,

Just add a -u to the installutil command.

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe -u "C:\Services\myservice.exe"
Tecla answered 13/3, 2014 at 8:34 Comment(1)
InstallUtil returns -1 if the command fails. You could rather check the error level with IF NOT '%ERRORLEVEL%' == '0' in this context.Schnapps
T
17

Perform the following:

  1. Start up the command prompt (CMD) with administrator rights.
  2. Type c:\windows\microsoft.net\framework\v4.0.30319\installutil.exe [your windows service path to exe]
  3. Press return and that's that!

It's important to open with administrator rights otherwise you may find errors that come up that don't make sense. If you get any, check you've opened it with admin rights first!

To open with admin rights, right click 'Command Prompt' and select 'Run as administrator'.

Source: http://coderamblings.wordpress.com/2012/07/24/how-to-install-a-windows-service-using-the-command-prompt/

Tamah answered 26/7, 2012 at 0:29 Comment(0)
A
15

Install Service:-

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" 
"C:\Services\myservice.exe"

UnInstall Sevice:-

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" -u "C:\Services\myservice.Service.exe"
Amygdala answered 1/11, 2017 at 12:52 Comment(0)
T
8

I must add one more point in this thread. To install/uninstall 64-bit version of assemblies one should use 64-bit version of tool. To install a service, the command should be:

"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe"
"C:\YourFolder\YourService.exe"

and to uninstall the command should be:

"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe" -u
"C:\YourFolder\YourService.exe"
Topple answered 15/12, 2017 at 12:29 Comment(0)
M
7

Create a *.bat file beside of your windows service exe file for installing with the following context:

CLS
ECHO Installing My Windows Service 

START %windir%\Microsoft.NET\Framework\v4.0.30319\installutil.exe "%~d0%~p0\YourWindowsServiceExeName.exe"

Create a *.bat file beside of your windows service exe file for uninstalling with the following context:

CLS
ECHO Uninstalling My Windows Service 

START %windir%\Microsoft.NET\Framework\v4.0.30319\installutil.exe -u "%~d0%~p0\YourWindowsServiceExeName.exe"

Run each of bat file as Admin to install or uninstall your windows service.

Mulish answered 7/7, 2018 at 11:54 Comment(0)
N
3
  1. Run Windows Command Prompt as Administrator
  2. paste this code: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\ to go to folder
  3. edit and run this too: installutil C:\ProjectFolder\bin\Debug\MyProject.exe

Note: To uninstall: installutil /u C:\ProjectFolder\bin\Debug\MyProject.exe

Nims answered 17/10, 2017 at 6:56 Comment(0)
E
3

open Developer command prompt as Admin and navigate to

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

Now use path where is your .exe there

InstallUtil "D:\backup\WindowsService\WindowsService1\WindowsService1\obj\Debug\TestService.exe"
Exogenous answered 11/3, 2020 at 6:12 Comment(0)
M
2

If you are using Powershell and you want to install .NET service you can use Install-Service module. It is a wrapper for InstalUtil tool.

It exposes 3 commands

  • Install-Service - invokes InstallUtil.exe pathToExecutable command
  • Install-ServiceIfNotInstalled - first it checks if service is installed if not perform the method Install-Service
  • Uninstall-Service- it uninstalls service. ServiceName of path to executable can be used.

Code to this module can be viewed here

Middleaged answered 15/2, 2018 at 19:26 Comment(0)
L
2

Open Visual studio and select new project by selecting Windows Service template in Windows Desktop tab. Than copy following code into your service_name.cs file.

using System.Diagnostics;
using System.ServiceProcess;
namespace TimerService
{
    public partial class Timer_Service : ServiceBase
    {
        public Timer_Service()
        {
            InitializeComponent();
        }
        static void Main()
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Timer_Service service = new Timer_Service();
                service.OnStart(null);
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                    new Timer_Service()
                };
                ServiceBase.Run(ServicesToRun);
            }
        }
        protected override void OnStart(string[] args)
        {
            EventLog.WriteEvent("Timer_Service", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Service start successfully." });
        }
        protected override void OnStop()
        {            
            EventLog.WriteEvent("Timer_Service", new EventInstance(0, 0, EventLogEntryType.Information), new string[] { "Service stop successfully." });
        }
    }
}

Right-Click on service_name.cs file and open designer of service. than right-click and select Add Installer. than right-click on serviceProcessInstaller1 and change its property value of Account from User to Local System.

Remove static void main method from Program.cs file. Than save and Build your project.

NOTE: goto bin\Ddebug folder of your project folder. Than open Properties of your service_name.exe file. Than goto Compatibility tab. Than click on Change Settings For All Users.

Select option Run this program as an administrator.

Now, You have to open CommandPromt as Administrator. After open, set directory to where your InstallUtil.exe file is placed. for ex: C:\Windows\Microsoft.NET\Framework64\v4.0.30319. now write the following command:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>InstallUtil.exe -i C:\TimerService\TimerService\bin\Debug\TimerService.exe

Note: -i is for install he service and -u for Unsinstall.

after -i set the write the path where you want to install your service.

now write the command in CommandPromt as follows:

C:\TimerService\TimerService\bin\Debug>net start service_name

Note: use stop for stop the Service.

Now, open ViewEventLog.exe. Select Windows Logs>Application. There you can check your Service's log by start and stop the service.

Lawrenson answered 10/8, 2018 at 14:3 Comment(0)
M
2

the following code , install and uninstall the Service,

Open the command prompt and run the program as an administrator and fire the below command and press enter.

Syntax

To Install

C:\windows\microsoft.net\framework\v4.0.30319>InstallUtil.exe + Your copied path + \your service name + .exe

eg :Our Path InstallUtil.exe C:\MyFirstService\bin\Debug\MyFirstService.exe

To uninstall

 C:\windows\microsoft.net\framework\v4.0.30319>InstallUtil.exe -u + Your copied path + \your service name + .exe

eg : Our path InstallUtil.exe -u C:\MyFirstService\bin\Debug\MyFirstService.exe

for more help you can see the following link: sample program

Musicology answered 16/9, 2018 at 7:23 Comment(0)
Y
1
  1. start up the command prompt (CMD) with administrator rights.
  2. Type c:\windows\microsoft.net\framework\v4.0.30319\installutil.exe [your windows service path to exe]
  3. Press return
Yangyangtze answered 14/4, 2017 at 10:8 Comment(0)
S
1

Follow these steps when deploying the Windows Service, don't lose time:

  1. Run command prompt by the Admin right

  2. Insure about release mode when compilling in your IDE

  3. Give a type to your project installer on design view

  4. Select authentication type in accordance the case

  5. Insure about software dependencies: If you are using a certificate install it correctly

  6. Go your console write this:

    C:\Windows\Microsoft.NET\Framework\yourRecentVersion\installutil.exe c:\yourservice.exe

there is a hidden -i argument before the exe path -i c:\ you can use -u for uninstallling

  1. Look your .exe path to seem log file. You can use event viewer to observing in the feature
Schulz answered 5/7, 2019 at 22:42 Comment(0)
Y
1

1.From the Start menu, select the Visual Studio directory, then select Developer Command Prompt for VS .

2.The Developer Command Prompt for Visual Studio appears.

3.Access the directory where your project's compiled executable file is located.

4.Run InstallUtil.exe from the command prompt with your project's executable as a parameter

Yvor answered 13/12, 2019 at 12:29 Comment(0)
S
0

when your assembly version and your Visual studio project Biuld setting on dot net 2 or 4 install with same version.

install service with installutil that same version

if build in dot net 4

Type c:\windows\microsoft.net\framework\v4.0.30319\installutil.exe

if build in dot net 2

Type c:\windows\microsoft.net\framework\v2.0.11319\installutil.exe

Siftings answered 5/4, 2017 at 12:31 Comment(0)
D
0

you can do using command prompt and write: C:\windows\microsoft.net\framework\v4.0.30319\InstallUtil.exe -i ".EXE file of window service"

Delius answered 5/4, 2021 at 7:44 Comment(0)
P
0

General steps for a clean manual install/re-install of a Windows Service:

  • Reference this answer, for instructions on setting up an installer in the project for your service
  • Reference this answer for general tips on achieving a correctly configured production service
  • Open a command prompt as administrator
  • If re-installing, first stop and uninstall the previously installed service via the command line.
  • Ensure a fresh build of the service is located on the target computer
  • If re-installing, restart the PC
  • Open the folder that contains the built .exe in the command prompt
  • Install the service via the command line
  • Start the service
  • If the service doesn't start in a timely fashion, see here (for a production service, ensure you're using the release build)
Puckett answered 12/5, 2023 at 1:28 Comment(0)
A
-1

You can use InstallUtil to install any windows service. 1: C:\Windows\Microsoft.NET\Framework64\v4.0.30319 in command prompt running as Adminstrator. 2: Copy the Exe path and type InstallUtil.exe "your exe path" and hit enter.

If you want visual elaboration. Goto below link. It helped me alot.

https://youtu.be/yrdyYxzI7SE

Axletree answered 30/5, 2021 at 22:6 Comment(0)
R
-2

You should open command prompt, go to

C:\windows\microsoft.net\framework\v4.0.30319\InstallUtil.exe -i ".EXE file of window service"

Rubbing answered 2/5, 2018 at 18:19 Comment(0)
S
-4

Open command prompt as administrator, go to your Folder where your .exe resides. To Install Exe as service

D:\YourFolderName\YourExeName /i

To uninstall use /u.

Sickle answered 29/9, 2019 at 7:33 Comment(1)
This works only if YourExeName.exe is coded to work this way, which is not a requirement for a service, it isn't even a standard practice. Unless you know that the OP's service .exe works this way, this is not a useful answer.Festatus

© 2022 - 2024 — McMap. All rights reserved.