Technical Anshu
Tutorials Blog
🛠️ Technician Script

Clean Temp Files on Many PCs With a Safe Batch Script

One PC is a chore; forty PCs is a script. This is the fleet version of the safe cleanup routine — five supported commands, a one-time preset, and the rules that keep it safe at scale.

✍️ Technical Anshu🪟 Windows 10 / 11⏱️ 6 minute read
🎥 The full cleanup routine is shown live in this video — every folder, every setting

Everything in this series' cleanup routine can be scripted — if the script sticks to the same safe doors and resists every clever addition. This page gives the exact batch file technicians deploy for monthly temp cleanup across an office, explains every line, and lists the commands that were deliberately left out.

clean-temp.bat — the fleet-safe script (copy exactly)
@echo off del /q/f/s %TEMP%\* 2>nul del /q/f/s C:\Windows\Temp\* 2>nul cleanmgr /sagerun:1 Dism.exe /Online /Cleanup-Image /StartComponentCleanup PowerShell -Command "Clear-RecycleBin -Force -ErrorAction SilentlyContinue"
👆 Five safe doors in script form — note what is deliberately NOT in it: Prefetch and WinSxS by hand
1

Why a Script, and Why This Script Is Deliberately Boring

Cleaning one PC takes five minutes. Cleaning forty PCs by hand takes a week of walking. A technician script exists to run the same safe doors everywhere — and boring is the whole point. Every line below maps to a supported Windows feature used elsewhere in this series. There are no registry edits, no ownership tricks, no Prefetch deletion and no folder that Windows protects.

Copy it exactly as shown into a file named clean-temp.bat. It must be run as administrator.

2

Line by Line — What Each Command Does

  1. del /q/f/s %TEMP%\* 2>nul — empties the running user's temp contents quietly; the 2>nul hides the expected "file in use" errors for locked files, which are skipped by design
  2. del /q/f/s C:\Windows\Temp\* 2>nul — the same for the system temp folder
  3. cleanmgr /sagerun:1 — runs Disk Cleanup with a saved preset (see the next section — the preset must be created once)
  4. Dism.exe /Online /Cleanup-Image /StartComponentCleanup — the supported way to slim the component store; it never deletes anything Windows still needs and it is the only WinSxS route allowed
  5. Clear-RecycleBin via PowerShell — empties the Bin for all users on the machine; the error-action flag keeps the script silent on PCs with an already-empty Bin
3

The One-Time Preset — sageset Before sagerun

The /sagerun:1 line only works after you define preset 1 once:

  1. On one representative PC, run cleanmgr /sageset:1
  2. Tick the safe categories — Temporary files, Thumbnails, Recycle Bin, DirectX Shader Cache, system error dumps — and leave Downloads and anything user-owned unticked
  3. Click OK. The preset is stored in that PC's registry, so on a fleet, deploy the preset with your management tool or Group Policy preferences alongside the script

Skip this step and /sagerun:1 simply does nothing — a silent no-op that has confused many first attempts.

4

Deploying It Across Many PCs — the Sensible Order

  1. Pilot first: run the script on two or three PCs, including one older machine, and confirm free space rises and nothing complains
  2. Push through your existing tool — the same endpoint management or deployment system you use for software (Group Policy startup scripts and scheduled tasks also work) as a monthly task running as SYSTEM or an admin account
  3. Log the result: add a line that writes the PC name and free space to a shared log — echo %COMPUTERNAME% done >> \\SERVER\Logs\cleanup.log — so "did it run" is a fact, not a hope
  4. Never schedule it during work hours on slow drives; the DISM line can take several minutes of disk activity
5

The Safety Rules That Travel With the Script

A script multiplies whatever it does — wisdom or mistakes — by the size of the fleet. So the rules stay attached:

  1. The script empties contents, never deletes the folders themselves
  2. Locked files are skipped on purpose; nobody adds force flags against system protection
  3. Prefetch is absent because deleting it slows launches — on forty PCs at once
  4. If a PC keeps refilling within days, pull it out of the schedule and diagnose it; the script is housekeeping, not a cure for a leaking application

🎥 Watch the Full Cleanup in the Video

See every cleanup location opened live on a real PC, in the same safe order, step by step.

▶️ Watch Full Video
❓ Frequently Asked Questions

Why does cleanmgr sagerun do nothing on my PCs?

Because the matching preset was never created. Run cleanmgr /sageset:1 once, tick the safe categories and click OK — that stores preset 1. Only then does /sagerun:1 execute it. On a fleet, the preset must exist on each PC, so deploy it with the script.

Is the DISM StartComponentCleanup line safe to run monthly?

Yes — it is Microsoft's supported component-store cleanup. It removes superseded component versions that are safe to remove and nothing else. It can take several minutes on a slow drive, which is why the script should run outside busy hours.

Can I add the Prefetch folder to the script for a deeper clean?

Do not. Deleting Prefetch slows application launches until the traces rebuild — on one PC that is a bad tip, and on a fleet it is a bad tip multiplied by every machine. The script's omissions are deliberate.

This guide is based on the Technical Anshu cache and temp cleanup video • Stuck on a step? Ask in the video comments — you will get a reply 🙏