Automating disk cleanup via Intune using the StartComponentCleanup task on a weekly basis

Colombo Chiara141 0 Reputation points
2026-07-03T11:10:48.9333333+00:00

Requirements:

  • Automate disk cleanup across the entire device fleet
  • Avoid any custom scripting or code development
  • Use the built-in Windows scheduled task StartComponentCleanup
  • Purpose of the task: reduce WinSxS folder size by removing superseded components

Question:

  • How can Intune be configured to execute this native maintenance task on a weekly schedule across all corporate endpoints?
Windows for business | Windows 365 Business
0 comments No comments

1 answer

Sort by: Most helpful
  1. Senthil kumar 1,295 Reputation points
    2026-07-03T11:25:19.1333333+00:00

    Hi @Colombo Chiara141

    Below command for power-shell execution

    $TaskName = "StartComponentCleanupWeekly"
    $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoLogo -WindowStyle Hidden -Command `"Dism.exe /Online /Cleanup-Image /StartComponentCleanup`""
    $Trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 3am
    $Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable
    Register-ScheduledTask -TaskName $TaskName -Action $Action -Trigger $Trigger -Settings $Settings -Description "Weekly Component Cleanup"
    Enable logs 
    $Log = "$env:ProgramData\StartComponentCleanup.log"
    $Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoLogo -WindowStyle Hidden -Command `"Dism.exe /Online /Cleanup-Image /StartComponentCleanup | Out-File -Append $Log`""
    

    Below command for batch execution

    normal batch script.

    @echo off
    setlocal
    rem ============================================================
    rem  Create Weekly StartComponentCleanup Scheduled Task
    rem  Safe for Intune, Windows 10/11, and Insider builds
    rem  Does NOT affect existing scripts or servicing stack
    rem ============================================================
    set "CleaningTaskName=StartComponentCleanupWeekly"
    set "LOGFILE=%ProgramData%\StartComponentCleanup.log"
    rem --- Delete old task if exists ---
    schtasks /Query /TN "%CleaningTaskName%" >nul 2>&1
    if %errorlevel%==0 (
        schtasks /Delete /TN "%CleaningTaskName%" /F >nul
    )
    rem --- Create new weekly task (Sunday 3 AM) ---
    schtasks /Create ^
     /TN "%CleaningTaskName%" ^
     /SC WEEKLY ^
     /D SUN ^
     /ST 03:00 ^
     /RU SYSTEM ^
     /RL HIGHEST ^
     /TR "cmd.exe /c Dism.exe /Online /Cleanup-Image /StartComponentCleanup >> \"%LOGFILE%\" 2>&1"
    echo Scheduled task '%CleaningTaskName%' created successfully.
    endlocal
    

    Thanks.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.