How to install and uninstall Windows services

By FoxLearn 12/10/2024 3:42:49 AM   42
To install a Windows service developed with the .NET Framework, you can use the `InstallUtil.exe` command-line utility or PowerShell.

For distributing the service to users with install/uninstall capabilities, you can use tools like the free WiX Toolset or commercial options such as Advanced Installer and InstallShield.

To use `InstallUtil.exe` to install your project, open the "Developer Command Prompt for VS" from the Start menu under Visual Studio.

Then, navigate to the directory containing your project's compiled executable file.

Finally, run `InstallUtil.exe` with the path to your executable as the argument.

installutil <yourproject>.exe

If you're using the Developer Command Prompt for Visual Studio, `InstallUtil.exe` is already included in the system path. Otherwise, you can add it to the path or use its full path, which is located in `%WINDIR%\Microsoft.NET\Framework[64]\<framework_version>` where .NET Framework is installed.

For example:

cd\
cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319
C:\Windows\Microsoft.NET\Framework64\v4.0.30319>installutil path_your_service

installutil

Uninstall using InstallUtil.exe utility

To uninstall your project, run `InstallUtil.exe` from the command prompt with the `/uninstall` option followed by the path to your project's executable.

installutil /uninstall <yourproject>.exe

If the executable for a service is deleted but the service still exists in the registry, use the `sc delete` command to remove the service entry from the registry.

Install using PowerShell

Press Win + X and select Windows PowerShell (Admin) from the menu.

install windows service using powershell

To create a new service, run the `New-Service` cmdlet, providing the service name and the path to your project's executable as arguments.

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

To remove a service, run the `Remove-Service` cmdlet, providing the name of the service as the argument.

Remove-Service -Name "YourServiceName"

Similarly, use the `sc delete` command to remove the service entry from the registry.

sc.exe delete "YourServiceName"