PowerShell Tutorials

How to Automate Disk Cleanup with PowerShell

Optimize your system’s performance with automated Disk Cleanup using PowerShell, ensuring thorough file removal across all user accounts effortlessly.

PowerShell-7

To run Disk Cleanup as the system user and clean up files for all user accounts on the computer using PowerShell, you can follow these steps:

  1. Run Disk Cleanup as System User: To perform Disk Cleanup with elevated privileges, use the schtasks command to schedule a task that runs as the system user. This task will be set to clean up system files and files from all user accounts.
  2. Execute Disk Cleanup: The cleanmgr utility can be used to run Disk Cleanup. However, there isn’t a direct way to specify all user accounts using cleanmgr. Instead, you can automate the process using the Dism (Deployment Imaging Service and Management Tool) command, which can clean up system components and Windows Update files.

Here’s an example PowerShell script to accomplish this:

# Define a scheduled task to run Disk Cleanup as the SYSTEM user
$taskName = "DiskCleanupSystem"
$action = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c cleanmgr /sagerun:1"
$trigger = New-ScheduledTaskTrigger -AtLogOn
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest

# Register the scheduled task
Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Principal $principal

# Run the scheduled task immediately
Start-ScheduledTask -TaskName $taskName

# Wait for the task to complete (adjust time as necessary)
Start-Sleep -Seconds 900

# Remove the scheduled task after it has run
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false

# Run DISM to clean up system components
Dism /Online /Cleanup-Image /StartComponentCleanup /ResetBase

# Additional steps to clean up other specific locations can be added here

How It Works:

  1. Creating a Scheduled Task:
    • The script creates a scheduled task that runs cleanmgr /sagerun:1 with the system user. You must run Disk Cleanup once manually (cleanmgr /sageset:1) to configure the settings.
  2. Running DISM:
    • The script runs the Dism command to clean up system components, which removes superseded Windows updates and reduces the size of the component store.

Notes:

  • Manual Setup for cleanmgr /sageset: Before running this script, run cleanmgr /sageset:1 in an elevated command prompt to select the files you want to clean up. This step configures the Disk Cleanup settings.
  • Permissions: Ensure you run this script with administrative privileges.
  • Timing: Adjust the sleep duration if the Disk Cleanup task requires more time to complete.

This script provides a framework for automating the Disk Cleanup process using PowerShell and should be adjusted based on specific requirements and environments.


Discover more from Patrick Domingues

Subscribe to get the latest posts sent to your email.

author avatar
Patrick Domingues

Leave a Comment

Stay Informed

Receive instant notifications when new content is released.