Skip to main content

How to Restart Internet Information Services (IIS) in Batch Script

Restarting Internet Information Services (IIS) is one of the most common administrative tasks performed on Windows web servers. Whether you are deploying updated application binaries, clearing a misbehaving worker process, applying configuration changes, or recovering from a memory leak, a clean IIS restart resolves a wide range of issues without requiring a full server reboot.

In this guide, we will explore multiple methods to restart IIS from a Batch Script, from the simple one-liner to production-grade scripts with logging, health checks, and graceful connection draining.

Method 1: Using IISRESET (The Standard Approach)

The iisreset command is the purpose-built tool for restarting all IIS services. It stops every IIS-related service, then starts them again in the correct dependency order.

@echo off
setlocal

:: Verify Admin Rights
net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Administrator privileges required.
pause
exit /b 1
)

echo Restarting IIS...

iisreset

if %errorlevel% equ 0 (
echo.
echo [SUCCESS] IIS has been restarted.
) else (
echo.
echo [ERROR] IIS restart failed. Check Event Viewer.
)

pause
endlocal

IISRESET Variations

CommandBehavior
iisresetFull restart (stop + start)
iisreset /restartIdentical to the above
iisreset /stopStop only (no restart)
iisreset /startStart only
iisreset /statusShow current service status
iisreset /timeout:60Allow up to 60 seconds for graceful shutdown
iisreset /rebootonerrorIf the restart fails, reboot the entire server

Method 2: Using NET Commands (Manual Sequencing)

If you need finer control over the restart sequence, or want to perform actions between the stop and start phases, use net stop and net start separately.

@echo off
setlocal

net session >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Admin required.
pause
exit /b 1
)

echo =============================================
echo IIS RESTART (Manual Sequence^)
echo =============================================
echo.

:: Phase 1: Stop
echo [1/3] Stopping W3SVC...
net stop W3SVC 2>nul
echo [1/3] Stopping WAS...
net stop WAS /y 2>nul

:: Phase 2: Perform maintenance (if needed)
echo.
echo [2/3] Maintenance window open.
echo (Deploy files, clear caches, etc.^)
timeout /t 3 /nobreak >nul

:: Phase 3: Start
echo.
echo [3/3] Starting WAS...
net start WAS
echo [3/3] Starting W3SVC...
net start W3SVC

echo.
echo [COMPLETE] IIS restart finished.
pause
endlocal

Why Use /y with WAS?

When stopping WAS, Windows prompts a confirmation because W3SVC (a dependent service) will also be stopped. The /y flag automatically confirms this, allowing the script to run non-interactively.

Method 3: Recycling Application Pools (Soft Restart)

A full IIS restart stops all websites on the server. In many cases, only one application is misbehaving. Recycling its specific application pool restarts just that application's worker processes without affecting other sites.

@echo off
setlocal

set "pool_name=DefaultAppPool"
set "appcmd=%SystemRoot%\System32\inetsrv\appcmd.exe"

echo Recycling Application Pool: %pool_name%

"%appcmd%" recycle apppool /apppool.name:"%pool_name%"

if %errorlevel% equ 0 (
echo [SUCCESS] %pool_name% recycled.
) else (
echo [ERROR] Failed to recycle %pool_name%.
)

pause
endlocal
tip

Recycling an application pool is much faster than a full iisreset and causes zero downtime for other websites on the same server. Use this when only a specific application needs attention.

Method 4: Production-Grade Restart Script

For servers running in production, a restart script should include pre-flight checks, logging, and post-restart health verification.

@echo off
title IIS Restart - Production
setlocal enabledelayedexpansion

:: Configuration
set "logdir=%~dp0"
set "health_url=http://localhost"
set "max_wait=30"

:: Build timestamp and log filename
for /f "tokens=2 delims==" %%T in ('wmic os get LocalDateTime /value') do set "dt=%%T"
set "ts=!dt:~0,4!-!dt:~4,2!-!dt:~6,2! !dt:~8,2!:!dt:~10,2!:!dt:~12,2!"
set "logfile=%logdir%iis_restart_!dt:~0,4!!dt:~4,2!!dt:~6,2!.log"

echo [!ts!] === IIS RESTART INITIATED === >> "!logfile!"

:: Admin check
net session >nul 2>&1
if !errorlevel! neq 0 (
echo [ERROR] Admin required.
echo [!ts!] ABORT: No admin rights >> "!logfile!"
pause
exit /b 1
)

:: Pre-flight: Check current status
echo [PRE-FLIGHT] Checking IIS status...
sc query W3SVC | findstr /i "RUNNING" >nul
if !errorlevel! equ 0 (
echo W3SVC: Running
echo [!ts!] PRE: W3SVC Running >> "!logfile!"
) else (
echo W3SVC: Not Running
echo [!ts!] PRE: W3SVC Not Running >> "!logfile!"
)

:: Perform restart
echo.
echo [RESTART] Executing iisreset...
echo [!ts!] Executing iisreset >> "!logfile!"

iisreset /timeout:30

set "restart_result=!errorlevel!"
echo [!ts!] iisreset exit code: !restart_result! >> "!logfile!"

if !restart_result! neq 0 (
echo [ERROR] iisreset reported a failure!
echo [!ts!] ERROR: iisreset failed >> "!logfile!"
pause
exit /b 1
)

:: Post-restart: Wait for services
echo.
echo [POST] Verifying services are running...
set /a "waited=0"

:verify_loop
sc query W3SVC | findstr /i "RUNNING" >nul
if !errorlevel! equ 0 goto services_ok

set /a "waited+=2"
if !waited! geq %max_wait% (
echo [ERROR] W3SVC did not reach RUNNING state in %max_wait% seconds!
echo [!ts!] ERROR: W3SVC timeout >> "!logfile!"
pause
exit /b 1
)
timeout /t 2 /nobreak >nul
goto verify_loop

:services_ok
echo W3SVC: Running
echo [!ts!] POST: W3SVC Running >> "!logfile!"

:: Health check: Try to reach the web server
echo.
echo [HEALTH] Testing HTTP response from %health_url%...
powershell -NoProfile -Command ^
"try {" ^
" $r = Invoke-WebRequest -Uri '%health_url%' -TimeoutSec 10 -UseBasicParsing;" ^
" Write-Host (' HTTP Status: ' + $r.StatusCode)" ^
"} catch {" ^
" Write-Host (' ERROR: ' + $_.Exception.Message)" ^
"}"

echo.
echo [!ts!] === RESTART COMPLETE === >> "!logfile!"
echo [DONE] IIS restart completed. Log: !logfile!
pause
endlocal

Method 5: Scheduled Restart

For servers that need periodic restarts to prevent memory leaks or clear stale caches, you can schedule the restart via Windows Task Scheduler.

@echo off
setlocal

:: Create a scheduled task to restart IIS every day at 3 AM
schtasks /create /tn "IIS Daily Restart" /tr "iisreset" /sc daily /st 03:00 /ru SYSTEM /rl highest /f

if %errorlevel% equ 0 (
echo [OK] Scheduled task "IIS Daily Restart" created.
echo IIS will restart every day at 3:00 AM.
) else (
echo [ERROR] Failed to create scheduled task.
)

pause
endlocal
warning

Scheduling an iisreset on a production server will cause a brief outage (typically 3-10 seconds). Always schedule it during the lowest-traffic window and ensure no long-running requests span the restart time.

Common Mistakes

The Wrong Way: Using TASKKILL on w3wp.exe

:: WRONG - Forcefully killing worker processes
taskkill /f /im w3wp.exe

Output Concern: Force-killing w3wp.exe worker processes bypasses the IIS process management layer. Active sessions are lost, in-memory data is corrupted, and IIS logs the termination as an unexpected crash. Use iisreset or appcmd recycle for proper lifecycle management.

The Wrong Way: Restarting the Entire Server

:: OVERKILL - A full server reboot is unnecessary for IIS
shutdown /r /t 0

An iisreset takes 3-10 seconds. A full server reboot takes 2-5 minutes and restarts every service on the machine. Only reboot the server when IIS-related issues persist after multiple restart attempts.

Best Practices

  1. Use iisreset for full restarts: It is atomic, handles dependencies, and reports success/failure reliably.
  2. Recycle app pools for targeted fixes: When only one application is problematic, recycle its pool instead of restarting the entire server.
  3. Log everything: Record timestamps, service states, and error codes for post-incident analysis.
  4. Health-check after restart: Verify the web server is responding to HTTP requests before declaring the restart successful.
  5. Use timeouts: Always specify a reasonable timeout to prevent the script from hanging indefinitely if a service refuses to stop.

Conclusion

Restarting IIS from a Batch Script ranges from a single iisreset command to a full production-grade workflow with pre-flight checks, graceful draining, and post-restart health verification. For targeted issues, recycling individual application pools via appcmd provides a surgical fix without impacting other sites. Regardless of the method chosen, the keys to a reliable restart are verifying administrator privileges, logging all actions, and confirming the services reached their expected state before the script exits.