Skip to main content

How to Uninstall an MSI Silently in Batch Script

Removing software reliably is a core part of the IT lifecycle. When a program reaches its "End of Life," or when you need to replace an old version with a fresh installer, you need a way to scrub the old package without disrupting the user. Manually finding the uninstaller in the Control Panel is too slow for a fleet of computers. A Batch script can use the msiexec engine to trigger a Silent Uninstallation. This ensures that the program, its registry keys, and its background services are removed cleanly and instantly, following the official "Wipe" logic defined by the software developer.

This guide will explain how to automate MSI removal in the background.

Method: The "Product Code" Purge (Msiexec)

Every MSI is identified by a unique Product Code (a GUID). This is the safest way to uninstall, as it doesn't matter if the program icon is missing or renamed.

@echo off
:: The GUID can be found in the registry or via 'wmic product get name, identifyingnumber'
set "ProductCode={4DBF0123-ABCD-1234-9876-0000000000AB}"
set "LogFile=C:\Logs\msi_uninstall.log"

echo [ACTION] Silently removing MSI package...

:: Ensure the log directory exists
if not exist "C:\Logs" mkdir "C:\Logs"

:: /x = Uninstall
:: /qn = Quiet mode (No user interface)
:: /norestart = Prevents sudden reboots
:: /L*V = Verbose logging for troubleshooting
start /wait "" msiexec /x "%ProductCode%" /qn /norestart /L*V "%LogFile%"

if %errorlevel% equ 0 (
echo [SUCCESS] Program uninstalled successfully.
) else if %errorlevel% equ 3010 (
echo [SUCCESS] Program uninstalled. A system restart is required.
) else if %errorlevel% equ 1605 (
echo [INFO] Product not found. It may already be removed.
) else (
echo [ERROR] Removal failed with exit code: %errorlevel%
echo Check the log file: %LogFile%
)

pause
warning

Administrative Rights. Uninstalling system-level software requires deep access to the registry and protected folders. You MUST run your script as an Administrator.

Method 2: Uninstalling from a Local MSI File

If you still have the original .msi file that you used to install the program, you can use that file as the uninstallation key.

@echo off
set "Source=C:\Deploy\Setup.msi"
set "LogFile=C:\Logs\msi_file_uninstall.log"

:: Verify the MSI file exists
if not exist "%Source%" (
echo [ERROR] MSI file not found: %Source%
echo The original installer file is required for this method.
echo Use Method 1 (Product Code) if the file is unavailable.
pause
exit /b 1
)

if not exist "C:\Logs" mkdir "C:\Logs"

echo [ACTION] Using source file to uninstall...

:: Passing the file path instead of the GUID
start /wait "" msiexec /x "%Source%" /qn /norestart /L*V "%LogFile%"

if %errorlevel% equ 0 (
echo [SUCCESS] Program uninstalled successfully.
) else if %errorlevel% equ 3010 (
echo [SUCCESS] Program uninstalled. Restart required.
) else (
echo [ERROR] Removal failed with exit code: %errorlevel%
echo Check the log file: %LogFile%
)

pause

Method 3: The "Rip and Replace" Migration

Use this if you need to uninstall an old version before installing a new one, ensuring the complete sequence runs unattended.

@echo off
set "OldProductCode={GUID-OF-OLD-APP}"
set "NewMSI=C:\Deploy\NewApp_v2.msi"
set "LogDir=C:\Logs"

echo [LOG] Initializing version migration...

if not exist "%LogDir%" mkdir "%LogDir%"

:: Step 1: Uninstall the old version and wait for completion
echo [STEP 1] Removing old version...
start /wait "" msiexec /x "%OldProductCode%" /qn /norestart /L*V "%LogDir%\old_version_remove.log"

if %errorlevel% equ 0 (
echo [OK] Old version removed.
) else if %errorlevel% equ 3010 (
echo [OK] Old version removed (restart pending).
) else if %errorlevel% equ 1605 (
echo [OK] Old version was not installed. Proceeding.
) else (
echo [ERROR] Failed to remove old version. Exit code: %errorlevel%
echo Check: %LogDir%\old_version_remove.log
pause
exit /b 1
)

:: Step 2: Verify the new installer exists
if not exist "%NewMSI%" (
echo [ERROR] New installer not found: %NewMSI%
pause
exit /b 1
)

:: Step 3: Install the new version
echo [STEP 2] Installing new version...
start /wait "" msiexec /i "%NewMSI%" /qn /norestart /L*V "%LogDir%\new_version_install.log"

if %errorlevel% equ 0 (
echo [SUCCESS] Migration complete. New version is installed.
) else if %errorlevel% equ 3010 (
echo [SUCCESS] Migration complete. Restart required to finalize.
) else (
echo [ERROR] New version installation failed. Exit code: %errorlevel%
echo Check: %LogDir%\new_version_install.log
)

pause

How to Avoid Common Errors

Wrong Way: Thinking the "Name" is enough

Running msiexec /x "Google Chrome" will not work for most applications. msiexec expects the internal "Identifying Number" (the Product Code) or a direct path to the original installer file.

Correct Way: Use the GUID (Method 1). If you don't know the GUID, run this command in a separate window to find it: wmic product get Name, IdentifyingNumber

Problem: User Account Control (UAC)

If you don't run the script as an Admin, a UAC prompt will appear even if you use the /qn switch. Since the UI is suppressed, the prompt might stay invisible in the taskbar, causing your script to hang forever while it waits for a "Yes" that never comes.

Solution: Always ensure your Batch script is running with elevated privileges before triggering msiexec.

Best Practices and Rules

1. Identify "Exit Code 1605"

If your script returns %errorlevel% of 1605, it means "This action is only valid for products that are currently installed." In other words, the program was already gone. Handle this as an informational result in your cleanup scripts (as shown in Methods 1 and 3).

2. Log the Error

If an uninstallation fails, the log file will tell you exactly which file or registry key is "Stuck." msiexec /x "%ProductCode%" /qn /L*V "C:\Logs\cleanup_error.log"

3. Check Service Locks

If the program has a background service (like an Antivirus or a VPN), stop that service using net stop before you try to uninstall the MSI. This prevents "File in Use" errors that might block the uninstallation.

Conclusions

Uninstalling MSI packages via Batch script is a vital task for any IT professional focused on workstation hygiene and security. By moving from manual Control Panel clicks to automated msiexec commands, you gain the visibility and efficiency needed to manage your software inventory at scale. This professional level of control ensures that your environment stays clean, updated, and free of legacy software that could pose a risk to your organization.