Purge aged temporary files from C:\Windows\Temp and every user profile on Windows endpoints, then empty the Recycle Bin
This Automox Worklet™ removes accumulated temporary files from every local user profile and from the system temp directory on a Windows endpoint, then clears the per-volume Recycle Bin. The Worklet enumerates user profiles with Get-CimInstance win32_userprofile, filters to paths under C:\Users, and combines those with the system-level C:\Windows\Temp folder to build the full target list.
Inside each user profile, the Worklet walks AppData\Local\Temp and AppData\Local\Temporary Internet Files. It scans each folder recursively with Get-ChildItem and selects items whose LastWriteTime is older than the configured $fileAge threshold. The default threshold is 30 days; setting $fileAge to 0 removes every file in the target paths regardless of age.
After the per-user sweep, the Worklet empties the Recycle Bin by piping the contents of C:\$Recycle.Bin through Remove-Item with -Force and -Recurse. This bypass of Clear-RecycleBin is intentional so the cleanup runs whether or not an interactive user is signed in. The script never touches files outside the named temp paths, so it is safe to run on production workstations and shared servers without an exclusion list.
Temporary files grow quietly on every Windows endpoint. Installer caches, browser working files, Office autosave fragments, and per-application scratch data land in AppData\Local\Temp and never get cleaned up by the application that wrote them. On a developer workstation or a Citrix host with dozens of profiles, the temp footprint can pass 20 GB before disk pressure surfaces as a help-desk ticket. The temp paths also hold stale credential caches, downloaded attachments, and decrypted document copies that should not survive past their working session.
Run this Worklet across the workstation, terminal server, and jump-host fleet to clear C:\Windows\Temp and every user's AppData\Local\Temp in a single policy pass, then keep it on a recurring schedule so a developer laptop, a finance jump host, and a terminal server with 40 active profiles each reclaim disk space on the same cadence. Reclamation results land in the Automox activity log with the line 'Directories cleaned and Recycling Bin emptied.', so disk recovery becomes a tracked operational metric instead of a quarterly fire drill.
Evaluation phase: The evaluation script exits non-zero, which flags every endpoint as out of compliance and schedules remediation on the next policy run. This design is deliberate for a maintenance Worklet: temporary files are always accumulating, so the evaluation does not attempt to set a clean-versus-dirty threshold. If you need conditional execution, gate the policy with a schedule (for example, weekly off-hours) rather than evaluation logic.
Remediation phase: The remediation script reads $fileAge from the policy variable, calls Get-CimInstance win32_userprofile to list local user profiles under C:\Users, and builds the target folder list (C:\Windows\Temp plus per-user AppData\Local\Temp and AppData\Local\Temporary Internet Files). For each folder, Get-ChildItem -Recurse selects items whose LastWriteTime is older than the threshold, and Remove-Item -Force -Recurse deletes them. The script then runs Get-ChildItem 'C:\$Recycle.Bin' -Force | Remove-Item -Recurse -Force to empty the Recycle Bin across volumes. The final Write-Output line confirms completion in the Automox activity log.
Windows 10, Windows 11, or Windows Server 2016 and later (workstation and server roles both supported)
PowerShell 5.1 or later, available by default on every supported Windows build
Local SYSTEM context for the Automox agent so Get-CimInstance and Remove-Item can reach every user profile under C:\Users, including profiles for users who are not currently signed in
$fileAge variable set in remediation.ps1 (default 30 days; set to 0 to clear every file regardless of age, or to a higher number such as 90 to be more conservative on shared workstations)
No third-party modules required; the Worklet uses only built-in cmdlets (Get-CimInstance, Get-ChildItem, Remove-Item, Write-Output)
After remediation completes, C:\Windows\Temp and each user's AppData\Local\Temp and AppData\Local\Temporary Internet Files contain only items younger than the configured $fileAge threshold. The Recycle Bin on every local volume is empty. The Automox activity log shows the line 'Directories cleaned and Recycling Bin emptied.' with exit code 0. Recovered space typically ranges from a few hundred megabytes on a freshly imaged workstation to several gigabytes on long-lived developer or terminal-server endpoints.
Verify by running Get-Volume C on the endpoint and comparing SizeRemaining before and after. You can also inspect a cleaned path directly with Get-ChildItem 'C:\Windows\Temp' | Measure-Object. For audit evidence, pair the run with a follow-up Worklet that captures Get-PSDrive C output and stores it with the Automox policy run identifier. The evaluation phase intentionally returns non-zero, so the next scheduled run will execute remediation again and pick up any new aged files. This is the intended behavior for a recurring maintenance policy.


Loading...
Consider Worklets your easy button
A Worklet is an automation script, written in Bash or PowerShell, designed for seamless execution on endpoints – at scale – within the Automox platform. Worklets deploy named-CVE mitigations within hours of disclosure, perform configuration, remediation, and install or remove applications and settings across Windows, macOS, and Linux.

AUTOMOX + WORKLETS™
Uncover new possibilities with simple, powerful automation.
By submitting this form you agree to our Master Services Agreement and Privacy Policy
By submitting this form you agree to our Master Services Agreement and Privacy Policy.
Already have an account? Log in