Windows Services: Install & Uninstall via InstallUtil.exe Command‑Line and Common Troubleshooting
1. Installing and Uninstalling Windows Services from Command‑Line
1. Tools
Microsoft provides InstallUtil.exe for installing and uninstalling .NET Windows services.
InstallUtil.exeis version‑specific to .NET Framework. Installation will fail if the tool version does not match your project’s target framework;- Copy the matching‑version
InstallUtil.exeinto your service program’sDebugoutput directory (same folder as the service EXE).
InstallUtil is part of the .NET SDK. Different SDK versions ship different copies of InstallUtil.
.NET Framework 4.0 / 4.5 / 4.6 / 4.7 / 4.8 (most widely used) has separate 32‑bit and 64‑bit builds.
Example path for 64‑bit: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe
.NET 5 /.NET 6 /.NET 7 /.NET 8 /.NET 9 (.NET Core family) do not include InstallUtil.exe
2. File‑folder Structure (Debug Directory)
WindowsServiceDemo.exe // Main Windows service executable
WindowsServiceDemo.pdb
InstallUtil.exe // Copied‑over installation utility
install.bat // Installation script
uninstall.bat // Uninstallation script
log.txtCode language: JavaScript (javascript)
3. Writing Batch Scripts
install.bat 【Install Service】
d:
cd D:\WindowsService3\WindowsService3\bin\Debug
InstallUtil WindowsServicePicture.exe
pauseCode language: CSS (css)
uninstall.bat 【Uninstall Service】
d:
cd D:\WindowsService3\WindowsService3\bin\Debug
InstallUtil /u WindowsServicePicture.exe
pause
Parameter explanation:
/u= uninstall, used to remove a service
You must right‑click and run CMD as administrator / right‑click the bat file and run as administrator; insufficient permissions trigger security exceptions, causing installation failure with automatic rollback.
2. Common Windows‑Service Installation Issues and Fixes
Root‑cause Summary
Two main reasons for failed installations:
- Mismatch between service project build platform, .NET version and InstallUtil.exe version;
- Lack of administrator privileges leading to system security block.
Exception 1:BadImageFormatException
Error Message
System.BadImageFormatException: Could not load file or assembly "WindowsServiceDemo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" or one of its dependencies. An attempt was made to load a program with an incorrect format.Code language: JavaScript (javascript)
Cause
Bitness mismatch between project build target platform(x86/x64/AnyCPU) and InstallUtil.exe; or incompatible .NET version.
Solutions
- Set your project to compile with
x86; - Use InstallUtil.exe matching your project framework version;
Exception 2:SecurityException Permission Error (Frequent)
Error Message
An exception occurred during the Install phase.
System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.
Starting the Rollback phase of the installation.
Installation failed, and rollback has been performed.Code language: CSS (css)
Full log‑file example:
H:\ProjectSource\WindowsServiceDemo\WindowsServiceDemo\bin\Debug\WindowsServiceDemo.InstallLogCode language: CSS (css)
Root Cause
The command‑line / Visual Studio lacks administrator rights and cannot read‑write system event logs.
Standard Fixes
- Visual Studio: Right‑click Run as administrator, open project and rebuild;
- CMD / bat script: Right‑click → Run as administrator;
- Set project build platform to
x86.
Successful‑installation Indicator
Console output:
The Commit phase completed successfully.
The transacted install has completed.
Open Windows service list(services.msc), you will see your newly registered service.
Service installation and uninstallation