Skip to content
Maintenance Batch Script

05/03/2025 - Add Restart and Clear Print Spooler, Install App updates.
04/22/2025 - Removed Delete TEMP files and added Disk Cleanup utility automation.

Maintenance Batch Script

I put off building a proper all in one Windows maintenance batch script for years. I had smaller scripts here and there to speed things up, but nothing consolidated. Starting this blog finally gave me a good reason to sit down, build a single script I actually use, and share it so anyone else can grab it.

 

This script is simple, practical, and includes a few small touches that make it easy to run again later.

 

Key Features

1 Automatic admin elevation

If you forget to run it as Administrator, it restarts itself with elevated permissions. No extra steps.

 

2 Clears conflicting processes

Right up front it stops common Windows servicing processes like DISM and TiWorker so maintenance tasks are not fighting for the same resources.

 

3 Flexible startup options

On launch you choose whether to continue with a reboot, without a reboot, or exit. You stay in control.

 

4 Live status updates

Every step reports what it is doing so you are not guessing about progress behind the scenes.

 

What It Does

After you pick your preferences, the script runs a set of routine Windows maintenance tasks:

 

  • Defrags and optimizes all hard drives
  • Cleans the TEMP directory
  • Checks for and installs Windows Updates
  • Performs a DISM cleanup
  • Runs sfc /scannow twice for good measure

 

The Script

The full batch script is below. Copy it into Notepad, save it as a .bat file, and run it when you need a quick tune up. Modify it to fit your workflow. It is a solid foundation for keeping a system in good shape with minimal effort.

 

@ECHO OFFSETLOCAL ENABLEEXTENSIONSCOLOR 09TITLE JOSHFRIDEY.COM SYSTEM MAINTENANCE BATCH:: --- Elevation Check ---:: Check if script is running as adminNET SESSION >NUL 2>&1IF %ERRORLEVEL% NEQ 0 (    ECHO Requesting administrative privileges...    PowerShell -Command "Start-Process '%~f0' -ArgumentList '/elevated' -Verb RunAs"    EXIT /B):: Check for elevation flagIF "%1"=="/elevated" SHIFT:: -- Show Menu --:MENUCLSECHO ====================================ECHO # SYSTEM MAINTENANCE OPTIONSECHO ====================================ECHO.ECHO [1] WINDOWS UPDATES WITHOUT REBOOTECHO [2] WINDOWS UPDATES WITH REBOOTECHO [3] EXIT SCRIPTECHO.CHOICE /C 123 /N /M "Choose an option: "IF ERRORLEVEL 3 GOTO :EOFIF ERRORLEVEL 2 SET FLAG=REBOOT&GOTO MAINTENANCEIF ERRORLEVEL 1 SET FLAG=NOREBOOT&GOTO MAINTENANCE:: --- Maintenance Logic ---:MAINTENANCECLSECHO ====================================ECHO # REBOOT DISM and TIWORKERECHO ====================================TASKLIST | FINDSTR "Dism.exe TiWorker.exe" >NUL && TASKKILL /F /IM "Dism.exe" /IM "TiWorker.exe" /T >NUL 2>&1ECHO.ECHO ====================================ECHO # Disk Cleanup AutomationECHO ====================================ECHO # Clearing CleanMgr.exe automation settings.powershell -Command "Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' -Name StateFlags0001 -ErrorAction SilentlyContinue | Remove-ItemProperty -Name StateFlags0001 -ErrorAction SilentlyContinue"ECHO # Enabling Update Cleanup. This is done automatically in Windows 10 via a scheduled task.powershell -Command "New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -Name StateFlags0001 -Value 2 -PropertyType DWord"ECHO # Enabling Temporary Files Cleanup.powershell -Command "New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -Name StateFlags0001 -Value 2 -PropertyType DWord"ECHO # Starting CleanMgr.exe...powershell -Command "Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -Wait"ECHO # Waiting for CleanMgr and DismHost processes. Second wait neccesary as CleanMgr.exe spins off separate processes.powershell -Command "Get-Process -Name cleanmgr,dismhost -ErrorAction SilentlyContinue | Wait-Process"ECHO.ECHO ====================================ECHO # INSTALLING PSWindowsUpdate MODULEECHO ====================================ECHO # Setting Execution Policy to Remote Signed...powershell -Command "Set-ExecutionPolicy RemoteSigned -Force -ErrorAction SilentlyContinue"ECHO # Installing Package Provider NuGet...powershell -Command "Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -ErrorAction SilentlyContinue"ECHO # Installing Module PSWindowsUpdate...powershell -Command "Install-Module PSWindowsUpdate -Force -ErrorAction SilentlyContinue"ECHO # Importing Module PSWindowsUpdate...powershell -Command "Import-Module PSWindowsUpdate -Force -ErrorAction SilentlyContinue"ECHO.ECHO ====================================ECHO # CHECKING FOR WINDOWS UPDATESECHO ====================================powershell -Command "Get-WindowsUpdate -ErrorAction SilentlyContinue"ECHO.ECHO ====================================ECHO # RESTART & CLEAR PRINT SPOOLERECHO ====================================net stop spoolertaskkill.exe /f /im printfilterpipelinesvc.exedel /F /Q %systemroot%\System32\spool\PRINTERS\*del /F /Q %systemroot%\System32\spool\SERVERS\*net start spoolerECHO ====================================ECHO # RUNNING DISM CLEANUPECHO ====================================DISM /Online /Cleanup-Image /RestoreHealth /StartComponentCleanupECHO.ECHO ====================================ECHO # RUNNING DISM RESTOREHEALTHECHO ====================================DISM /Online /Cleanup-Image /RestoreHealthECHO ====================================ECHO # RUNNING SFCECHO ====================================SFC /SCANNOWSFC /SCANNOWECHO.ECHO ====================================ECHO # RUNNING DEFRAGECHO ====================================DEFRAG /C /O /UECHO.ECHO ====================================ECHO # INSTALLING APP UPDATESECHO ====================================winget update --force --allECHO ====================================ECHO # INSTALLING WINDOWS UPDATESECHO ====================================IF "%FLAG%"=="REBOOT" (    powershell -Command "Install-WindowsUpdate -AcceptAll -Install -AutoReboot -ErrorAction SilentlyContinue"    shutdown -r -t 00) ELSE (    powershell -Command "Install-WindowsUpdate -AcceptAll -Install -IgnoreReboot -ErrorAction SilentlyContinue")ECHO.:FINISHECHO.ECHO === MAINTENANCE COMPLETE ===ECHO Press any key to exit...PAUSE >NULEXIT /B
Scratch Contact Us

You can contact my best friend and food provider with this form. Suggestions, corrections, and questions are always welcome! Please also message me French fries...

Scratch