๐Ÿ“ฑ 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, clears SoftwareDistribution and catroot2, restarts the services, and invokes cleanmgr /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 โ€” cleanmgr signals completion even if nothing was actually cleaned.
  • cleanmgr /VERYLOWDISK is 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 the StateFlags registry keys under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\* and use cleanmgr /sagerun:1 instead.
  • Deleting SoftwareDistribution cancels 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.All is 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: true unless 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

PermissionWhy It's Needed
DeviceManagementManagedDevices.Read.AllLook up the target device's Intune managedDevice ID, compliance state, and storage info
DeviceManagementManagedDevices.PrivilegedOperations.AllTrigger a syncDevice action so the device picks up the newly assigned script
DeviceManagementScripts.ReadWrite.AllCreate and assign the PowerShell script object in Intune (required specifically โ€” Configuration.ReadWrite.All is insufficient)
Device.Read.AllResolve the corresponding Entra ID device object to add as a group member
Group.ReadWrite.AllCreate 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

Deploy a one-time PowerShell script via Intune to device {device_name} that stops Windows Update services, clears the SoftwareDistribution and catroot2 folders, restarts the services, and runs Disk Cleanup. Target only that device, trigger a sync so it runs promptly, and report back the script ID and group so I can clean them up afterward.
Try in Dex CoAdmin

For End Users

How an employee would ask Dex for help

My work computer is really slow and running low on disk space โ€” can someone clean it up?
Try in Dex Playground