Restart IIS Application Pool Using Batch Script and Task Scheduler

Windows September 18, 2026 38 Views 3 min read
Restart IIS Application Pool Using Batch Script and Task Scheduler

Restart IIS Application Pool Using a Batch Script

This Windows batch script recycles a specific IIS Application Pool and writes the result to a log file. It is useful when an application pool needs to be restarted automatically on a schedule.

What This Script Does

  • Targets one IIS Application Pool.
  • Creates the log folder if it does not exist.
  • Recycles the Application Pool using appcmd.
  • Logs the restart time.
  • Logs whether the recycle succeeded or failed.

1. Configure the Application Pool

Change this value to your IIS Application Pool name:

set APPPOOL=hello.domain.online

The log file is written to:

C:\DailyDbBackup\Logs\app_pool_restart.log

2. Complete Batch Script

Save the script as iis_app_pool_restart.bat:

@echo off

set APPPOOL=hello.domain.online
set LOGFILE=C:\script\Logs\app_pool_restart.log

:: Create log directory if it doesn't exist
if not exist "C:\script\Logs" mkdir "C:\script\Logs"

echo ================================================== >> "%LOGFILE%"
echo [%date% %time%] Restarting Application Pool: %APPPOOL% >> "%LOGFILE%"

%windir%\System32\inetsrv\appcmd recycle apppool /apppool.name:"%APPPOOL%" >> "%LOGFILE%" 2>&1

if %errorlevel%==0 (
    echo [%date% %time%] SUCCESS - Application Pool recycled successfully. >> "%LOGFILE%"
) else (
    echo [%date% %time%] ERROR - Failed to recycle Application Pool. >> "%LOGFILE%"
)

echo. >> "%LOGFILE%"

3. Test the Script Manually

Open Command Prompt as Administrator and run:

iis_app_pool_restart.bat

After running it, check the log:

C:\DailyDbBackup\Logs\app_pool_restart.log

A successful run will contain a message similar to:

SUCCESS - Application Pool recycled successfully.

4. How the Restart Works

%windir%\System32\inetsrv\appcmd recycle apppool /apppool.name:"%APPPOOL%"

The IIS appcmd utility sends a recycle command to the specified Application Pool.

5. Schedule the Script with Windows Task Scheduler

You can run the batch file automatically every day, every hour, or at another required time using Task Scheduler.

Step 1 — Open Task Scheduler

Win + R
taskschd.msc
Enter

Step 2 — Create a Task

In Task Scheduler:

  1. Click Create Task.
  2. Enter a name such as IIS App Pool Restart.
  3. Select Run whether user is logged on or not.
  4. Select Run with highest privileges.

Step 3 — Set the Trigger

Open the Triggers tab and click New. Choose when the Application Pool should restart.

For example:

Begin the task: On a schedule
Daily
Start: 02:00 AM
Recur every: 1 day

Step 4 — Set the Action

Open the Actions tab and click New. Choose:

Action: Start a program

In Program/script, enter:

C:\Windows\System32\cmd.exe

In Add arguments, enter the full path to the batch file:

/c "C:\Scripts\iis_app_pool_restart.bat"

Replace the path with the actual location of your batch file.

Step 5 — Save the Task

Click OK. Windows may ask for the password of the account that will run the task.

The task will now run automatically according to the trigger you configured.

6. Test the Scheduled Task

In Task Scheduler:

  1. Find IIS App Pool Restart.
  2. Right-click the task.
  3. Click Run.
  4. Wait a few seconds.
  5. Check the IIS Application Pool and the log file.

7. Quick Reference

# Test batch file manually
iis_app_pool_restart.bat

# Open Task Scheduler
taskschd.msc

8. Important Notes

  • Run the task with an account that has permission to recycle the IIS Application Pool.
  • Use Run with highest privileges when required by your server configuration.
  • Test the batch file manually before scheduling it.
  • Check the log file after the scheduled run.
  • Make sure the Application Pool name exactly matches IIS.

Conclusion

This simple batch script lets you recycle an IIS Application Pool and keep a timestamped success/failure log. Windows Task Scheduler can then run the script automatically at the required time.

Discussion (0)