๐ฑ Recipe ยท Intune & Device Management
Deploy a One-Time PowerShell Script via Intune to Run Disk Cleanup and Clear Windows Update Cache
Remotely reclaim disk space on a managed Windows device by pushing a targeted PowerShell script through Intune
Complexity
Intermediate
Impact
device-performance + storage + windows-update + remote-remediation + intune-scripts
Context
Why This Matters
Why this recipe matters
When a user reports their Windows device is running slowly, one of the most common root causes is critically low free disk space โ especially when the Windows Update cache (C:\Windows\SoftwareDistribution) and catroot2 folder have grown large, or when temp files, update leftovers, and component store bloat have accumulated over months.
Rather than asking the end user to run Disk Cleanup manually (which many won't do correctly or at all), Intune admins can deploy a one-time PowerShell script that performs the cleanup silently in the SYSTEM context and runs at the next device check-in.
When to run this recipe
- A user reports slow performance and the device shows less than ~15% free disk space
- Windows Update failures or stuck downloads suggest a corrupted update cache
- You want to reclaim space across many devices without touching each one
- You need a quick remediation action that doesn't require a reboot or visit to the user's desk
Expected Outcomes
What you'll have when you finish
- A reusable Intune PowerShell script (
DiskCleanup-WUCacheReset.ps1) that stops Windows Update services, clearsSoftwareDistributionandcatroot2, restarts the services, and invokescleanmgr /VERYLOWDISK - A targeted assignment scoped to a single device (via a temporary security group) so only the intended machine runs the cleanup
- A triggered device sync so the script executes at the next check-in rather than waiting for the 1-hour Intune script polling cycle
- A clear audit trail in the Intune portal showing the script, its assignment, and per-device run results
Risks & Considerations
Warnings and gotchas
- Intune scripts run only once per device per assignment. If the script fails, you must re-create it (or change the script ID) to re-run it โ
cleanmgrsignals completion even if nothing was actually cleaned. cleanmgr /VERYLOWDISKis non-interactive and uses the default cleanup categories. It will not run if the disk is not actually low. For more aggressive cleanup, pre-populate theStateFlagsregistry keys underHKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*and usecleanmgr /sagerun:1instead.- Deleting
SoftwareDistributioncancels in-progress Windows Updates. Any partially downloaded updates will be re-downloaded on the next update scan. Don't run this during a critical patch cycle. - Permissions required are specific. Creating the script requires
DeviceManagementScripts.ReadWrite.AllโDeviceManagementConfiguration.ReadWrite.Allis NOT sufficient, despite what the Graph docs sometimes imply. - Script assignments target groups, not individual devices. Create a dedicated security group (or use a dynamic-membership group) and delete it after execution to keep your tenant tidy.
- Do not set
enforceSignatureCheck: trueunless your org signs all deployed scripts โ otherwise the script will never run. - The script runs as SYSTEM by default. Do not put user-specific paths (e.g.
$env:USERPROFILE) in it โ they will resolve to the SYSTEM profile.
Required Permissions
| Permission | Why It's Needed |
|---|---|
| DeviceManagementManagedDevices.Read.All | Look up the target device's Intune managedDevice ID, compliance state, and storage info |
| DeviceManagementManagedDevices.PrivilegedOperations.All | Trigger a syncDevice action so the device picks up the newly assigned script |
| DeviceManagementScripts.ReadWrite.All | Create and assign the PowerShell script object in Intune (required specifically โ Configuration.ReadWrite.All is insufficient) |
| Device.Read.All | Resolve the corresponding Entra ID device object to add as a group member |
| Group.ReadWrite.All | Create a temporary security group and add the device as a member so the script can be assigned |
The fastest way to get this done โ just ask Dex. Copy the prompt below and paste it into your Dex conversation.
For IT Admins
Paste into Dex CoAdmin
For End Users
How an employee would ask Dex for help