Home   tech  

Scheduled task to clean up the disk in windows 10 and windows 11

Here's a PowerShell script that you can use to perform routine disk cleanup tasks in Windows. This script will delete temporary files, empty the Recycle Bin, and perform other cleanup tasks to free up disk space:

First set execution policy in powershell

Set-ExecutionPolicy Bypass -Scope Process

Then run below script in powershell

# Set the list of cleanup tasks
$cleanupTasks = @(
    "UpdateCache",
    "WindowsUpdate",
    "WindowsUpgrade",
    "DownloadedProgramFiles",
    "RecycleBin",
    "TempSetupFiles",
    "Thumbnails",
    "UserFileVersions",
    "BranchCache",
    "LogFiles"
)

# Run Disk Cleanup for each task
foreach ($task in $cleanupTasks) {
    Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:$task" -Wait
}

# Display a message indicating the cleanup is complete
Write-Output "disk cleanup completed."

If you want to run a PowerShell script automatically and you need to bypass the execution policy, you can create a scheduled task to run your PowerShell script and bypass the execution policy. When creating the scheduled task, you can specify the -ExecutionPolicy Bypass argument to run the script with the desired execution policy. Here's how you can do it:

   $Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-ExecutionPolicy Bypass -File "C:\path\to\your\script.ps1"'
  
Published on: Aug 02, 2023, 04:19 AM  
 

Comments

Add your comment