22.7 C
New York
Monday, July 29, 2024

How you can Automate Disk Cleanup with PowerShell


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

PowerShell-7

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:

  1. Run Disk Cleanup as System Person: To carry out Disk Cleanup with elevated privileges, use the schtasks command 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.
  2. Execute Disk Cleanup: The cleanmgr utility can be utilized to run Disk Cleanup. Nonetheless, there isn’t a direct approach to specify all consumer accounts utilizing cleanmgr. As a substitute, you may automate the method utilizing the Dism (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:

  1. Making a Scheduled Activity:
    • The script creates a scheduled process that runs cleanmgr /sagerun:1 with the system consumer. You need to run Disk Cleanup as soon as manually (cleanmgr /sageset:1) to configure the settings.
  2. Operating DISM:
    • The script runs the Dism command to scrub up system parts, which removes outdated Home windows updates and reduces the dimensions of the part retailer.

Notes:

  • Guide Setup for cleanmgr /sageset: Earlier than operating this script, run cleanmgr /sageset:1 in 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.


Uncover extra from Patrick Domingues

Subscribe to get the newest posts despatched to your e mail.

author avatar



Supply hyperlink

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles