Windows cmd install service

To install a Windows service via the command line, you can use the `sc create` command followed by the service name and the executable path.

sc create "YourServiceName" binPath= "C:\Path\To\YourServiceExecutable.exe"

Prerequisites

Before you begin the process to install a Windows service via CMD, it’s essential to ensure you have the right setup.

System Requirements: Ensure your Windows version meets the minimum requirements needed to create and manage Windows Services. In most cases, Windows 7, 8, 10, and later versions support service creation.

Permissions Required: Installing a Windows service requires administrator rights. This ensures you can create system-level applications that run in the background without user intervention.

Install Windows From Cmd: A Quick How-To Guide

Install Windows From Cmd: A Quick How-To Guide

Understanding Windows Services

A Windows Service is a robust application that runs in the background instead of a standard user interface. Services provide essential functionality such as server operations, system monitoring, and performing scheduled tasks automatically.

Unlike traditional applications, Windows Services have specific characteristics: they start when the Windows operating system boots and are independent of user sessions. This means they can continue running regardless if a user is logged into the system.

Common use cases for Windows Services include:

  • Antivirus services that scan for threats in real-time.
  • Backup services that automatically save files at scheduled intervals.
  • Web server services which host applications and websites.

Delete Windows Service Cmd: A Simple Guide

Delete Windows Service Cmd: A Simple Guide

Installing a Windows Service Using CMD

Preparing to Create a Service

Setting Up Your Environment: The first step in creating your service involves ensuring you have all necessary tools installed. For example, if you’re writing a service in C#, you will need the .NET Framework SDK installed.

Creating Your Service Application: Your service application should be designed to follow the Windows Service model. This usually requires some programming knowledge. C# is a common choice for Windows Services, but you can also use Java and other languages.

Compiling Your Service Application

Once you’ve written your service code (for instance, in a file named `MyService.cs`), you’ll need to compile it into an executable (.exe) format.

Use the command line to compile your service by employing the C# compiler (`csc`). Here’s how:

csc /target:exe /out:MyService.exe MyService.cs

In this command:

  • `/target:exe` specifies that you’re creating an executable file.
  • `/out:MyService.exe` indicates the output file name.
  • The last parameter is the source code file you previously created.

Installing the Service via CMD

Now that your service application is compiled, you can proceed to install your Windows service using CMD. The SC (Service Control) command is the primary tool used for this purpose.

Here’s how to use it:

sc create MyService binPath= "C:\Path\To\MyService.exe"

Breaking down the command:

  • `MyService` is the name you are assigning to your service.
  • `binPath=` specifies the full path to where your compiled service executable is stored.

Alternative Installation Methods

If you want an alternative to the SC command, consider using NSSM (Non-Sucking Service Manager), which manages services in a user-friendly way, especially for applications that are not designed to be Windows Services natively.

To use NSSM:

  1. First, install NSSM by following the documentation on its official site.
  2. Once installed, run the following command to install your service:
nssm install MyService "C:\Path\To\MyService.exe"

The NSSM utility automatically handles many of the common pitfalls associated with service management.

Verifying Installation

After you’ve successfully created your Windows Service, it’s crucial to verify its installation. You can accomplish this by checking the service status using:

sc query MyService

This command will return the current status of your service, including whether it is ‘RUNNING’ or ‘STOPPED’.

Install Windows 10 Cmd: Quick and Easy Guide

Install Windows 10 Cmd: Quick and Easy Guide

Managing Your Service

Starting and Stopping Your Service

To manage your service after installation, use the SC command to start or stop it.

To start your service, use:

sc start MyService

For stopping the service, the command is:

sc stop MyService

These commands allow you to control the service as needed without needing to navigate complex GUI settings.

Deleting the Service

If you ever need to remove your Windows service, you can do this easily via CMD. Use the following command:

sc delete MyService

Be cautious when deleting a service; ensure that it’s not currently in use or required for critical operations.

Uninstall Service Cmd: A Quick Guide to Mastery

Uninstall Service Cmd: A Quick Guide to Mastery

Debugging and Troubleshooting

If you encounter issues during or after installation, several common pitfalls can arise.

Common Installation Issues: Ensure you have the necessary administrative rights. If you receive permission errors, you may need to run the command prompt as an administrator.

Service Log Files: Windows Services typically create log files that can help in debugging. To find logs related to your service, check the Event Viewer by typing in `eventvwr` in CMD and navigating to Windows Logs > Application. Look for entries related to your service to glean insights about any errors.

Mastering Windows Services Cmd: A Quick Guide

Mastering Windows Services Cmd: A Quick Guide

Conclusion

This guide outlines a straightforward approach to install a Windows service via CMD. By following these detailed steps, you can successfully create, manage, and debug your own Windows Services. Understanding these concepts not only enhances your command line skills but also opens the door to powerful automation and system management techniques. As you progress, consider exploring further CMD commands related to service management for an even deeper insight into Windows Services.

This article is explaining about how to Install/Create and Delete/Remove a Windows Service using Command Prompt. You can use the Service Control Manager’s utility command sc to Install and Delete Windows Service.

Note: Run the command prompt with elevated privileges(Run as administrator) to use the command sc.

Use the below command to install a Windows Service.

sc create [service-name] binpath= [servic-file-path]

service-name : Name of new Windows Service.

servic-file-path : File path of Windows Service file

sc create "MorganTechService" binpath= "C:\Program FilesMorganTechSPacemyservice.exe"

Install and Remove Windows Service using Command Prompt

Delete Windows Service using Command Prompt

Use the below command to uninstall a Windows Service.

sc delete "MorganTechService"

Delete Windows Service using Command Prompt

In this article I’ll show how to install and configure a Windows Service, query its status and properties, and then how to uninstall it. I’ll be using the command line utilities installutil.exe and sc.exe to install and configure the service.

Add a service installer class to your Windows Service project

In order to use installutil.exe to install your Windows Service, you need to add a service installer class. Otherwise when you try to install, you’ll get the following error:

No public installers with the RunInstallerAttribute.Yes attribute could be found in the C:\Services\TestService.exe assembly.

To create a service installer class:

  • Open your service class file in design mode in Visual Studio.
  • Right-click > click Add Installer.
Service component design mode, right-click and click Add Installer

After you click Add Installer, it will create the service installer class and open it in design mode.

At a bare minimum, you need to set the ServiceName and Account properties.

  • Click serviceInstaller1 to bring up its properties.
  • Specify the ServiceName property.
After adding the installer, the ProjectInstaller.cs component will pop up in design mode. Click on serviceInstaller1 and fill in the ServiceName property

Next, set the Account property. This property is the account that your service runs as, so choose whatever makes sense in your case (if you’re not sure, ask a security person).

  • Click serviceProcessInstaller1 to bring up its properties.
  • Set the Account to whatever value is appropriate in your situation.
In ProjectInstaller.cs, click serviceProcessInstaller and set the appropriate service account

The following batch file installs TestService.exe by using installutil.exe, configures the service with sc.exe, and starts the service using net start:

@ECHO OFF

REM Get log file name with timestamp
for /f %%a in ('wmic os get LocalDateTime ^| findstr ^[0-9]') do (set ts=%%a)
set LogName="C:\logs\installTestService%ts:~0,8%%ts:~8,4%%ts:~12,2%.log"

REM Install service
set servicePath="C:\Services\TestService.exe" 
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" %servicePath% >> %LogName%

REM Add dependency on SQL Server
sc config TestService depend=MSSQL$SQLEXPRESS >> %LogName%

REM Make start automatically
sc config TestService start= auto >> %LogName%

REM On crash, restart after 1 minute
sc failure TestService actions= restart/60000/restart/60000// reset= 86400 >> %LogName%

REM Start the service
net start TestService >> %LogName%
Code language: plaintext (plaintext)

Running this installs the service and outputs to a timestamped log file (ex: C:\logs\installTestService20210205074016.log).

Note: Execute this by using Run as Administrator.

It configures the service with the following properties:

  • Depends on service MSSQL$SQLEXPRESS (SQL Server Express). This means it won’t run unless SQL Server Express is running. When SQL Server Express restarts, it also restarts TestService.
  • Starts automatically.
  • Restarts after 1 minute if the service crashes. Note: This is only configuring it to auto-restart twice. After two failures, it won’t attempt to auto-restart. It resets the failure counter after 1 day.

Query the service properties and status

You can look at the service properties and status either by looking in Services, or by querying the properties from the command line by using sc.exe.

Execute the following to get the service properties and status:

sc qc TestServiceCode language: plaintext (plaintext)

This shows that the service is running and it’s using the configuration specified in the batch install script (from the previous section):

[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: TestService
        TYPE               : 10  WIN32_OWN_PROCESS
        START_TYPE         : 2   AUTO_START
        ERROR_CONTROL      : 1   NORMAL
        BINARY_PATH_NAME   : "C:\Services\TestService.exe"
        LOAD_ORDER_GROUP   :
        TAG                : 0
        DISPLAY_NAME       : TestService
        DEPENDENCIES       : MSSQL$SQLEXPRESS
        SERVICE_START_NAME : NT AUTHORITY\LocalService
Code language: plaintext (plaintext)

Execute the following to check the failure configuration:

sc qfailure TestServiceCode language: plaintext (plaintext)

This shows that it’s using the failure configuration specified in the batch install script:

[SC] QueryServiceConfig2 SUCCESS

SERVICE_NAME: TestService
        RESET_PERIOD (in seconds)    : 86400
        REBOOT_MESSAGE               :
        COMMAND_LINE                 :
        FAILURE_ACTIONS              : RESTART -- Delay = 60000 milliseconds.
                                       RESTART -- Delay = 60000 milliseconds.

Uninstall the service

The following batch file uninstalls the service and outputs the results to a timestamped log:

@ECHO OFF

REM Get log file name with timestamp
for /f %%a in ('wmic os get LocalDateTime ^| findstr ^[0-9]') do (set ts=%%a)
set LogName="C:\logs\uninstallTestService%ts:~0,8%%ts:~8,4%%ts:~12,2%.log"

REM Install service
set servicePath="C:\Services\TestService.exe" 
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" %servicePath% -u >> %LogName%
Code language: plaintext (plaintext)

Running this uninstalls the service and outputs to a timestamped log file (ex: C:\logs\uninstallTestService20210205074023.log).

You can check if the service is uninstalled by checking if sc query returns an error, like this:

C:\WINDOWS\system32>sc query TestService
[SC] EnumQueryServicesStatus:OpenService FAILED 1060:

The specified service does not exist as an installed service.Code language: plaintext (plaintext)

Related Articles

The .net framework provides and command-line installer utility for installing services on the Windows system. You can use installutil.exe to install the Windows service via the command line. For .net 4 framework it’s available under C:\Windows\Microsoft.NET\Framework\v4.0.30319 directory.

First of all, navigate to the .net framework directory and then run installutil.exe followed by the Windows server exe file path.

cd "C:\Windows\Microsoft.NET\Framework\v4.0.30319\" 
installutil.exe 
 C:\Users\Rahul\WindowsService\bin\Debug\WindowsService.exe" 

This will install service on your system. In case it prompts for the authentication, Input the username, and password of your Windows system. Remember the user name must start with “.\” followed by username as shown in the below screen.

See the onscreen messages and check if the command successfully completed.

How to Uninstall Windows Service

To Uninstall the Windows service using “-u” option with the same command as installation. The command to uninstall the service looks like the below:

cd "C:\Windows\Microsoft.NET\Framework\v4.0.30319\" 
installutil.exe -u "C:\Users\Rahul\WindowsService\bin\Debug\WindowsService.exe" 

Conclusion

This tutorial helped you to install the service on the Windows system using the command prompt. Also provides you instructions to delete existing services. Next, you may like to read about managing Windows service via command line.

Introduction

This blog describes simple way to install Windows Service in local machine and remove windows service from local machine.

Getting Started

windows service installation is as simple as installing software, but is bit difference from software installation.

InstallUtil.exe utility is required to install Windows Service and you will find this utility from below path if installed Visual Studio. This utility is varying for a different version of the .NET framework of Visual Studio and System, you need to select correct utility based on your framework version in which you have built your Windows Service.

Microsoft provides variety of InstallUtil.exe which based on .NET FRAMEWORK, following below are the path of InstallUtil.exe for different version.

Utility Paths: For 32-bit Machine

.Net Framework 4 and above: C:\Windows\Microsoft.NET\Framework\v4.0.30319
.Net Framework 3.5: C:\Windows\Microsoft.NET\Framework\v3.5
.Net Framework 3.0: C:\Windows\Microsoft.NET\Framework\v3.0
.Net Framework 2.0: C:\Windows\Microsoft.NET\Framework\v2.0.50727
.Net Framework 1.0: C:\Windows\Microsoft.NET\Framework\v1.0.3705
.Net Framework 1.1: C:\Windows\Microsoft.NET\Framework\v1.1.4322

Utility Paths: For 64-bit Machine

.Net Framework 4 and above: C:\Windows\Microsoft.NET\Framework64\v4.0.30319
.Net Framework 3.5: C:\Windows\Microsoft.NET\Framework64\v3.5
.Net Framework 3.0: C:\Windows\Microsoft.NET\Framework64\v3.0
.Net Framework 2.0: C:\Windows\Microsoft.NET\Framework64\v2.0.50727
.Net Framework 1.0: C:\Windows\Microsoft.NET\Framework64\v1.0.3705
.Net Framework 1.1: C:\Windows\Microsoft.NET\Framework64\v1.1.4322

Precaution

  1. Make sure you have added Service Installer to your service application .
  2. If you not added go to Solution Explorer. In Solution Explorer, access Design view for the service for which you want to add an installation component.
  3. Click the background of the designer to select the service itself, rather than any of its contents.
  4. With the designer in focus, right-click, and then click Add Installer. A new class, ProjectInstaller, and two installation components, ServiceProcessInstaller and ServiceInstaller, are added to your project, and property values for the service are copied to the components.
  5. Click the ServiceInstaller component and verify that the value of the ServiceName property is set to the same value as the ServiceName property on the service itself.
  6. To determine how your service will be started, click the ServiceInstaller component and set the StartType(Manual, Automatic, Disabled) property to the appropriate value.
  7. Click the ServiceProcessInstaller component and set the appropriate property values.

If you have installed Visual Studio in your local machine or server, you can install Windows Service through Developer Command Prompt or you can install using system command Prompt. Go through the below steps to install Windows Service.

Steps to Install Windows Service

  1. Build your service application with release mode and take the builds int to a path, this will be path of your installed windows service.
  2. Open Developer Command Prompt execute the InstallUtil.exe with command
  3. The syntax of command is:
     C:\>"Full path of installutil.exe" "Full path of service.exe"  
    
  4. Here I will install windows service with .NET Framework version 4.0, hence command for my service will be
     C:\>"C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe" "Full path of service.exe"  
    
  5. Use the above command and press Enter

Steps to Uninstall Windows Service

Uninstalling service syntax is same as installation, but little difference like below.

 C:\>C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe -u "C:\Services\myservice.exe"  

Thanks

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Отсутствует подключение к удаленному рабочему столу windows 10
  • Windows xp and 98 remix
  • Как установить подсистему ubuntu на windows 10
  • Принудительное обновление windows 10 через командную строку
  • Заставка мыльные пузыри windows 10