Optimize your system’s efficiency with automated Disk Cleanup utilizing PowerShell, guaranteeing thorough file elimination throughout all consumer accounts effortlessly.

To run Disk Cleanup because the system consumer and clear up information for all consumer accounts on the pc utilizing PowerShell, you may comply with these steps:
- Run Disk Cleanup as System Person: To carry out Disk Cleanup with elevated privileges, use the
schtaskscommand to schedule a process that runs because the system consumer. This process can be set to scrub up system information and information from all consumer accounts. - Execute Disk Cleanup: The
cleanmgrutility can be utilized to run Disk Cleanup. Nonetheless, there isn’t a direct approach to specify all consumer accounts utilizingcleanmgr. As a substitute, you may automate the method utilizing theDism(Deployment Imaging Service and Administration Software) command, which might clear up system parts and Home windows Replace information.
Right here’s an instance PowerShell script to perform this:
# Outline a scheduled process to run Disk Cleanup because the SYSTEM consumer $taskName = "DiskCleanupSystem" $motion = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c cleanmgr /sagerun:1" $set off = New-ScheduledTaskTrigger -AtLogOn $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest # Register the scheduled process Register-ScheduledTask -TaskName $taskName -Motion $motion -Set off $set off -Principal $principal # Run the scheduled process instantly Begin-ScheduledTask -TaskName $taskName # Look ahead to the duty to finish (alter time as essential) Begin-Sleep -Seconds 900 # Take away the scheduled process after it has run Unregister-ScheduledTask -TaskName $taskName -Verify:$false # Run DISM to scrub up system parts Dism /On-line /Cleanup-Picture /StartComponentCleanup /ResetBase # Further steps to scrub up different particular areas might be added right here
How It Works:
- Making a Scheduled Activity:
- The script creates a scheduled process that runs
cleanmgr /sagerun:1with the system consumer. You need to run Disk Cleanup as soon as manually (cleanmgr /sageset:1) to configure the settings.
- The script creates a scheduled process that runs
- Operating DISM:
- The script runs the
Dismcommand to scrub up system parts, which removes outdated Home windows updates and reduces the dimensions of the part retailer.
- The script runs the
Notes:
- Guide Setup for cleanmgr /sageset: Earlier than operating this script, run
cleanmgr /sageset:1in an elevated command immediate to pick out the information you wish to clear up. This step configures the Disk Cleanup settings. - Permissions: Make sure you run this script with administrative privileges.
- Timing: Alter the sleep length if the Disk Cleanup process requires extra time to finish.
This script gives a framework for automating the Disk Cleanup course of utilizing PowerShell and needs to be adjusted based mostly on particular necessities and environments.
Associated
Uncover extra from Patrick Domingues
Subscribe to get the newest posts despatched to your e mail.


