
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.
1@ECHO OFF2SETLOCAL ENABLEEXTENSIONS3COLOR 094TITLE JOSHFRIDEY.COM SYSTEM MAINTENANCE BATCH5:: --- Elevation Check ---6:: Check if script is running as admin7NET SESSION >NUL 2>&18IF %ERRORLEVEL% NEQ 0 (9 ECHO Requesting administrative privileges...10 PowerShell -Command "Start-Process '%~f0' -ArgumentList '/elevated' -Verb RunAs"11 EXIT /B12)13:: Check for elevation flag14IF "%1"=="/elevated" SHIFT15:: -- Show Menu --16:MENU17CLS18ECHO ====================================19ECHO # SYSTEM MAINTENANCE OPTIONS20ECHO ====================================21ECHO.22ECHO [1] WINDOWS UPDATES WITHOUT REBOOT23ECHO [2] WINDOWS UPDATES WITH REBOOT24ECHO [3] EXIT SCRIPT25ECHO.26CHOICE /C 123 /N /M "Choose an option: "27IF ERRORLEVEL 3 GOTO :EOF28IF ERRORLEVEL 2 SET FLAG=REBOOT&GOTO MAINTENANCE29IF ERRORLEVEL 1 SET FLAG=NOREBOOT&GOTO MAINTENANCE30:: --- Maintenance Logic ---31:MAINTENANCE32CLS33ECHO ====================================34ECHO # REBOOT DISM and TIWORKER35ECHO ====================================36TASKLIST | FINDSTR "Dism.exe TiWorker.exe" >NUL && TASKKILL /F /IM "Dism.exe" /IM "TiWorker.exe" /T >NUL 2>&137ECHO.38ECHO ====================================39ECHO # Disk Cleanup Automation40ECHO ====================================41ECHO # Clearing CleanMgr.exe automation settings.42powershell -Command "Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' -Name StateFlags0001 -ErrorAction SilentlyContinue | Remove-ItemProperty -Name StateFlags0001 -ErrorAction SilentlyContinue"43ECHO # Enabling Update Cleanup. This is done automatically in Windows 10 via a scheduled task.44powershell -Command "New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -Name StateFlags0001 -Value 2 -PropertyType DWord"45ECHO # Enabling Temporary Files Cleanup.46powershell -Command "New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -Name StateFlags0001 -Value 2 -PropertyType DWord"47ECHO # Starting CleanMgr.exe...48powershell -Command "Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -Wait"49ECHO # Waiting for CleanMgr and DismHost processes. Second wait neccesary as CleanMgr.exe spins off separate processes.50powershell -Command "Get-Process -Name cleanmgr,dismhost -ErrorAction SilentlyContinue | Wait-Process"51ECHO.52ECHO ====================================53ECHO # INSTALLING PSWindowsUpdate MODULE54ECHO ====================================55ECHO # Setting Execution Policy to Remote Signed...56powershell -Command "Set-ExecutionPolicy RemoteSigned -Force -ErrorAction SilentlyContinue"57ECHO # Installing Package Provider NuGet...58powershell -Command "Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -ErrorAction SilentlyContinue"59ECHO # Installing Module PSWindowsUpdate...60powershell -Command "Install-Module PSWindowsUpdate -Force -ErrorAction SilentlyContinue"61ECHO # Importing Module PSWindowsUpdate...62powershell -Command "Import-Module PSWindowsUpdate -Force -ErrorAction SilentlyContinue"63ECHO.64ECHO ====================================65ECHO # CHECKING FOR WINDOWS UPDATES66ECHO ====================================67powershell -Command "Get-WindowsUpdate -ErrorAction SilentlyContinue"68ECHO.69ECHO ====================================70ECHO # RESTART & CLEAR PRINT SPOOLER71ECHO ====================================72net stop spooler73taskkill.exe /f /im printfilterpipelinesvc.exe74del /F /Q %systemroot%\System32\spool\PRINTERS\*75del /F /Q %systemroot%\System32\spool\SERVERS\*76net start spooler77ECHO ====================================78ECHO # RUNNING DISM CLEANUP79ECHO ====================================80DISM /Online /Cleanup-Image /RestoreHealth /StartComponentCleanup81ECHO.82ECHO ====================================83ECHO # RUNNING DISM RESTOREHEALTH84ECHO ====================================85DISM /Online /Cleanup-Image /RestoreHealth86ECHO ====================================87ECHO # RUNNING SFC88ECHO ====================================89SFC /SCANNOW90SFC /SCANNOW91ECHO.92ECHO ====================================93ECHO # RUNNING DEFRAG94ECHO ====================================95DEFRAG /C /O /U96ECHO.97ECHO ====================================98ECHO # INSTALLING APP UPDATES99ECHO ====================================100winget update --force --all101ECHO ====================================102ECHO # INSTALLING WINDOWS UPDATES103ECHO ====================================104IF "%FLAG%"=="REBOOT" (105 powershell -Command "Install-WindowsUpdate -AcceptAll -Install -AutoReboot -ErrorAction SilentlyContinue"106 shutdown -r -t 00107) ELSE (108 powershell -Command "Install-WindowsUpdate -AcceptAll -Install -IgnoreReboot -ErrorAction SilentlyContinue"109)110ECHO.111:FINISH112ECHO.113ECHO === MAINTENANCE COMPLETE ===114ECHO Press any key to exit...115PAUSE >NUL116EXIT /B
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...
