
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’ll admit it — after all these years working in IT, I never sat down to create a proper all-in-one maintenance batch script for myself. Sure, I’ve written and used smaller scripts here and there to speed things up, but nothing fully consolidated. Starting this blog finally gave me a reason to improve my personal toolkit — and better yet, share it with anyone who might find it useful.
This batch script is simple, practical, and includes a few neat features that make it easy to run and reuse:
Key Features
1 Automatic Admin Elevation
If you forget to run the script as Administrator, no worries — it automatically restarts itself with elevated permissions.
2 Clears Up Running Processes
It kills common Windows servicing processes like DISM
and TiWorker
right up front to avoid any potential conflicts.
3 Flexible Startup Options
Right at launch, you’ll get a prompt to choose whether you want to continue with a reboot, without a reboot, or just exit the script altogether.
4 Live Status Updates
Each step in the script gives you clear updates as it runs — so you’re never left guessing what’s happening behind the scenes.
What It Does
Once you’ve selected your preferences, the script takes care of the following routine maintenance tasks:
Defrags and optimizes all hard drives
Cleans out the TEMP directory
Checks for and installs Windows Updates
Performs a DISM cleanup
Runs sfc /scannow twice for good measure
The Script
You’ll find the full batch script below. You can copy and paste it straight into Notepad, save it as a .bat
file, and run it as needed. Feel free to modify it to suit your workflow — it’s a great foundation for keeping your system in good shape with minimal effort.
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
COLOR 09
TITLE JOSHFRIDEY.COM SYSTEM MAINTENANCE BATCH
:: --- Elevation Check ---
:: Check if script is running as admin
NET SESSION >NUL 2>&1
IF %ERRORLEVEL% NEQ 0 (
ECHO Requesting administrative privileges...
PowerShell -Command "Start-Process '%~f0' -ArgumentList '/elevated' -Verb RunAs"
EXIT /B
)
:: Check for elevation flag
IF "%1"=="/elevated" SHIFT
:: -- Show Menu --
:MENU
CLS
ECHO ====================================
ECHO # SYSTEM MAINTENANCE OPTIONS
ECHO ====================================
ECHO.
ECHO [1] WINDOWS UPDATES WITHOUT REBOOT
ECHO [2] WINDOWS UPDATES WITH REBOOT
ECHO [3] EXIT SCRIPT
ECHO.
CHOICE /C 123 /N /M "Choose an option: "
IF ERRORLEVEL 3 GOTO :EOF
IF ERRORLEVEL 2 SET FLAG=REBOOT&GOTO MAINTENANCE
IF ERRORLEVEL 1 SET FLAG=NOREBOOT&GOTO MAINTENANCE
:: --- Maintenance Logic ---
:MAINTENANCE
CLS
ECHO ====================================
ECHO # REBOOT DISM and TIWORKER
ECHO ====================================
TASKLIST | FINDSTR "Dism.exe TiWorker.exe" >NUL && TASKKILL /F /IM "Dism.exe" /IM "TiWorker.exe" /T >NUL 2>&1
ECHO.
ECHO ====================================
ECHO # Disk Cleanup Automation
ECHO ====================================
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 MODULE
ECHO ====================================
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 UPDATES
ECHO ====================================
powershell -Command "Get-WindowsUpdate -ErrorAction SilentlyContinue"
ECHO.
ECHO ====================================
ECHO # RESTART & CLEAR PRINT SPOOLER
ECHO ====================================
net stop spooler
taskkill.exe /f /im printfilterpipelinesvc.exe
del /F /Q %systemroot%\System32\spool\PRINTERS\*
del /F /Q %systemroot%\System32\spool\SERVERS\*
net start spooler
ECHO ====================================
ECHO # RUNNING DISM CLEANUP
ECHO ====================================
DISM /Online /Cleanup-Image /RestoreHealth /StartComponentCleanup
ECHO.
ECHO ====================================
ECHO # RUNNING DISM RESTOREHEALTH
ECHO ====================================
DISM /Online /Cleanup-Image /RestoreHealth
ECHO ====================================
ECHO # RUNNING SFC
ECHO ====================================
SFC /SCANNOW
SFC /SCANNOW
ECHO.
ECHO ====================================
ECHO # RUNNING DEFRAG
ECHO ====================================
DEFRAG /C /O /U
ECHO.
ECHO ====================================
ECHO # INSTALLING APP UPDATES
ECHO ====================================
winget update --force --all
ECHO ====================================
ECHO # INSTALLING WINDOWS UPDATES
ECHO ====================================
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.
:FINISH
ECHO.
ECHO === MAINTENANCE COMPLETE ===
ECHO Press any key to exit...
PAUSE >NUL
EXIT /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...