แก้ไข

แชร์ผ่าน


How to: Install and uninstall Windows services

Note

This article doesn't apply to hosted services in .NET. For the latest content on Windows services using Microsoft.Extensions.Hosting.BackgroundService and the Worker Service template, see:

If you're developing a Windows service with .NET Framework, you can quickly install your service app by using the InstallUtil.exe command-line utility or PowerShell. If you want to release a Windows service that users can install and uninstall, use the free WiX Toolset or commercial tools like Advanced Installer and InstallShield. For more information, see Create an installer package (Windows desktop).

Warning

If you want to uninstall a service that you didn't develop from your computer, don't follow the steps in this article. Instead, find out which program or software package installed the service, and then choose Apps in Settings to uninstall that program. Many services are integral parts of Windows; if you remove them, you might cause system instability.

To use the steps in this article, you first need to add a service installer to your Windows service. For more information, see Walkthrough: Creating a Windows service app.

You can't run Windows service projects directly from the Visual Studio development environment by pressing F5. Before you can run the project, you must install the service in the project.

Tip

You can use Server Explorer to verify that you've installed or uninstalled your service.

Install using InstallUtil.exe utility

  1. Open Developer Command Prompt for VS.

    From the Start menu, select All apps, expand Visual Studio <version>, and then select Developer Command Prompt for VS <version>.

  2. Navigate to the directory where your project's compiled executable file is located.

  3. Run InstallUtil.exe from the command prompt with your project's executable as the argument:

    installutil <yourproject>.exe
    

    If you're using the Developer Command Prompt for Visual Studio, InstallUtil.exe is already on the system path. Otherwise, you can add it to the path, or use the fully qualified path to invoke it. This tool is installed with .NET Framework in %WINDIR%\Microsoft.NET\Framework[64]\<framework_version>.

Uninstall using InstallUtil.exe utility

  1. Open Developer Command Prompt for VS.

    From the Start menu, select All apps, expand Visual Studio <version>, and then select Developer Command Prompt for VS <version>.

  2. Run InstallUtil.exe from the command prompt with the /uninstall option and your project's executable:

    installutil /uninstall <yourproject>.exe
    
  3. After the executable for a service is deleted, the service might still be present in the registry. If that's the case, use the command sc delete to remove the entry for the service from the registry.

Install using PowerShell

  1. From the Start menu, search for Windows PowerShell and then select it.

  2. Navigate to the directory where your project's compiled executable file is located.

  3. Run the New-Service cmdlet with a service name and your project's executable as arguments:

    New-Service -Name "YourServiceName" -BinaryPathName <yourproject>.exe
    

Uninstall using PowerShell

  1. From the Start menu, search for Windows PowerShell and then select it.

  2. Run the Remove-Service cmdlet with the name of your service as an argument:

    Remove-Service -Name "YourServiceName"
    

    Note

    You must have PowerShell 6 or later to use this cmdlet. For information about updating PowerShell, see Installing PowerShell on Windows.

  3. After the executable for a service is deleted, the service might still be present in the registry. If that's the case, use the command sc delete to remove the entry for the service from the registry.

    sc.exe delete "YourServiceName"
    

See also