Long before PowerShell existed, Windows automation ran on batch files — plain text scripts interpreted by cmd.exe. They’re clunky by modern standards, but they still ship with every Windows install, require zero setup, and remain the fastest way to automate a handful of repetitive commands.
The Basics: Variables, Loops, and Conditionals
Batch syntax is idiosyncratic, but the core building blocks map to what you’d expect from any scripting language, just with more percent signs.
@echo off
setlocal
set "TARGET_DIR=C:\Temp\Logs"
set "DAYS_OLD=30"
if not exist "%TARGET_DIR%" (
echo Directory not found: %TARGET_DIR%
exit /b 1
)
echo Deleting files older than %DAYS_OLD% days in %TARGET_DIR%...
forfiles /p "%TARGET_DIR%" /s /m *.log /d -%DAYS_OLD% /c "cmd /c del @path"
echo Done.
endlocal
Scheduling It
Pair a batch file with Task Scheduler and you have a zero-dependency maintenance job that survives even on a locked-down machine where PowerShell execution policy is restricted:
schtasks /create /tn "Nightly Log Cleanup" /tr "C:\Scripts\cleanup.bat" /sc daily /st 02:00
When to Reach for PowerShell Instead
Batch files are fine for simple file operations and launching other programs, but they fall apart quickly once you need real data structures, error handling, or API calls. Treat batch scripting as a tool for small, self-contained jobs — anything more ambitious belongs in PowerShell or Python.