How to Restore IIS Configuration in Batch Script
When a configuration change goes wrong, a site goes down, or an automated deployment corrupts the IIS settings, the ability to quickly restore a known-good configuration backup can mean the difference between minutes and hours of downtime. IIS includes a built-in restoration mechanism via appcmd that restores the server to a previously backed-up state in seconds.
In this guide, we will explore how to restore IIS configuration backups from a Batch Script using both the appcmd restore feature and manual file replacement methods.
Prerequisites
- A backup must already exist (created with
appcmd add backupor manual file copy). - Administrator privileges are required.
- Restoring will overwrite the current configuration. All changes made since the backup will be lost.
Method 1: Listing Available Backups
Before restoring, you need to know which backups are available.
@echo off
setlocal
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
:: Verify Admin
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Administrator privileges required.
pause
exit /b 1
)
echo Available IIS Configuration Backups:
echo ====================================
echo.
"%appcmd%" list backup
echo.
echo Backup location: %SystemRoot%\System32\inetsrv\backup\
pause
endlocal
Sample Output:
BACKUP "Backup_20240115_1430"
BACKUP "pre-ssl-change_20240116"
BACKUP "DailyBackup_20240117"
Method 2: Restoring from an APPCMD Backup
The appcmd restore backup command replaces the current applicationHost.config with the backed-up version.
@echo off
setlocal
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
set "backup_name=Backup_20240115_1430"
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo Restoring IIS configuration from: %backup_name%
echo.
echo WARNING: This will overwrite the current IIS configuration!
echo All changes made since this backup will be lost.
echo.
set "confirm="
set /p "confirm=Type YES to proceed: "
if /i not "%confirm%" == "YES" (
echo [CANCELLED] No changes made.
pause
exit /b 0
)
:: Create a safety backup of current configuration before overwriting
echo.
echo Creating pre-restore safety backup...
"%appcmd%" add backup "pre-restore" >nul 2>&1
:: Stop IIS before restoration for a clean swap
echo Stopping IIS...
iisreset /stop >nul
:: Restore the backup
echo Restoring configuration...
"%appcmd%" restore backup "%backup_name%"
if %errorlevel% neq 0 (
echo [ERROR] Restoration failed.
echo Starting IIS with current configuration...
iisreset /start
pause
exit /b 1
)
echo [SUCCESS] Configuration restored from "%backup_name%".
:: Restart IIS
echo Starting IIS...
iisreset /start
if %errorlevel% equ 0 (
echo [OK] IIS is back online with restored configuration.
) else (
echo [WARNING] IIS may not have started correctly. Check Event Viewer.
)
pause
endlocal
Why Stop IIS First?
While appcmd restore backup can work without stopping IIS, performing the restoration with IIS stopped ensures:
- No active requests are interrupted.
- No file locks prevent the configuration file from being replaced.
- IIS reloads the restored configuration cleanly on startup.
Method 3: Interactive Restoration Tool
For administrators who need to browse available backups and select one:
@echo off
title IIS Configuration Restore
setlocal enabledelayedexpansion
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
net session >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] Run as Administrator.
pause
exit /b 1
)
:menu
cls
echo =============================================
echo IIS CONFIGURATION RESTORE
echo =============================================
echo.
echo Available Backups:
echo.
:: Clear previous backup list entries
for /f "tokens=1 delims==" %%V in ('set backup[ 2^>nul') do set "%%V="
set "count=0"
for /f "tokens=1,*" %%A in ('"%appcmd%" list backup 2^>nul') do (
set /a "count+=1"
set "backup[!count!]=%%~B"
echo !count!. %%~B
)
if !count! equ 0 (
echo No backups found.
echo Create a backup first: appcmd add backup "MyBackup"
pause
exit /b 1
)
echo.
echo 0. Exit
echo.
set "choice="
set /p "choice=Select backup number to restore: "
if not defined choice goto menu
if "!choice!" == "0" (
endlocal
exit /b 0
)
:: Validate choice
call set "selected=%%backup[!choice!]%%"
if not defined selected (
echo [ERROR] Invalid selection.
pause
goto menu
)
echo.
echo You selected: !selected!
echo.
echo This will OVERWRITE the current IIS configuration.
set "confirm="
set /p "confirm=Type YES to confirm: "
if /i not "!confirm!" == "YES" (
echo [CANCELLED]
pause
goto menu
)
:: Create a pre-restore safety backup
echo.
echo Creating pre-restore safety backup...
"%appcmd%" add backup "pre-restore" >nul 2>&1
:: Perform restoration
echo Stopping IIS...
iisreset /stop >nul 2>&1
echo Restoring "!selected!"...
"%appcmd%" restore backup "!selected!"
if !errorlevel! equ 0 (
echo [SUCCESS] Restored.
) else (
echo [ERROR] Restoration failed.
)
echo Starting IIS...
iisreset /start
pause
goto menu
Method 4: Manual File Restoration
If you backed up the configuration files manually (not through appcmd), you can restore by copying them back.
@echo off
setlocal enabledelayedexpansion
set "iis_config=%SystemRoot%\System32\inetsrv\config"
set "backup_dir=D:\Backups\IIS\2024-01-15_1430"
net session >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
:: Verify backup exists
if not exist "%backup_dir%\applicationHost.config" (
echo [ERROR] Backup not found at: %backup_dir%
pause
exit /b 1
)
echo Restoring IIS configuration from manual backup...
echo Source: %backup_dir%
echo Target: %iis_config%
echo.
set "confirm="
set /p "confirm=Type YES to proceed: "
if /i not "!confirm!" == "YES" (
echo [CANCELLED]
pause
exit /b 0
)
:: Stop IIS
echo Stopping IIS...
iisreset /stop >nul
:: Back up the current (broken) config before overwriting
echo Saving current config as "pre-restore"...
"%SystemRoot%\System32\inetsrv\appcmd.exe" add backup "pre-restore" >nul 2>&1
:: Restore the files
echo Restoring configuration files...
copy /y "%backup_dir%\applicationHost.config" "%iis_config%\" >nul
copy /y "%backup_dir%\administration.config" "%iis_config%\" >nul 2>&1
copy /y "%backup_dir%\redirection.config" "%iis_config%\" >nul 2>&1
:: Start IIS
echo Starting IIS...
iisreset /start
if !errorlevel! equ 0 (
echo [SUCCESS] Configuration restored and IIS is running.
) else (
echo [ERROR] IIS failed to start. The restored config may be invalid.
echo Reverting to pre-restore backup...
"%SystemRoot%\System32\inetsrv\appcmd.exe" restore backup "pre-restore"
iisreset /start
if !errorlevel! equ 0 (
echo [OK] Reverted to pre-restore state.
) else (
echo [CRITICAL] Revert also failed. Manual intervention required.
)
)
pause
endlocal
Before manually overwriting configuration files, always create a backup of the current (possibly broken) configuration. This gives you a fallback if the restoration itself introduces problems.
Method 5: Deleting Old Backups
To manage disk space, remove backups that are no longer needed:
@echo off
setlocal
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"
set "backup_name=OldBackup_20231201"
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)
echo Deleting backup: %backup_name%
"%appcmd%" delete backup "%backup_name%"
if %errorlevel% equ 0 (
echo [OK] Backup deleted.
) else (
echo [ERROR] Backup not found or deletion failed.
)
pause
endlocal
Common Mistakes
The Wrong Way: Restoring Without Stopping IIS
:: RISKY - Active connections may interfere
"%appcmd%" restore backup "MyBackup"
Output Concern: While the command may succeed, active worker processes may cache the old configuration in memory. Stopping IIS before restoration and restarting afterward ensures a clean configuration reload.
The Wrong Way: Not Creating a Pre-Restore Backup
:: WRONG - No safety net if the restored backup is also broken
iisreset /stop
"%appcmd%" restore backup "SomeOldBackup"
iisreset /start
If the restored backup is invalid or outdated, you have no way to get back to the previous state. Always create a backup of the current configuration (even if it is broken) before overwriting it.
The Wrong Way: Restoring Across Different IIS Versions
:: WRONG - IIS 8.5 backup may not work on IIS 10
:: Copying applicationHost.config from Server 2012 R2 to Server 2019
Configuration files are version-specific. Schema changes between IIS versions can cause the restored file to be rejected. Only restore backups created on the same IIS version.
Best Practices
- Stop IIS before restoring: Prevents in-memory caching conflicts and file lock issues.
- Back up before restoring: Create a "pre-restore" snapshot so you can reverse the restoration if needed.
- Test on staging first: Validate restored configurations on a non-production server before applying to production.
- Match IIS versions: Only restore backups between identical IIS versions to avoid schema incompatibilities.
- Verify after restoring: Run
appcmd list siteand perform an HTTP health check to confirm the restoration was successful.
Conclusion
Restoring IIS configuration from a Batch Script is a straightforward appcmd restore backup command, but doing it safely requires a disciplined approach. Stopping IIS before the restoration, creating a pre-restore safety backup, confirming the operation interactively, and verifying the result afterward ensures that recovery operations are reliable and reversible. Combined with the backup practices covered in the companion guide, administrators have a complete backup-and-restore workflow for IIS disaster recovery.