Tuesday, February 13, 2024

#109: How to pediodically cleanup recycle bin using Powershell

Sometime, we accidently delete a big file that is left over in recycle bin. Whether you are using a server or laptop, this might be useful.
You can use PowerShell to periodically clean up the Recycle Bin by deleting its contents. Here's a script that you can schedule to run at specified intervals to perform this cleanup:

# Function to clean up Recycle Bin

function Clean-RecycleBin {

    # Get Recycle Bin items

    $recycleBinItems = Get-ChildItem -Path $env:USERPROFILE\RecycleBin -Force

    # Delete Recycle Bin items
    foreach ($item in $recycleBinItems) {
        Remove-Item -Path $item.FullName -Force -Recurse
    }

    Write-Output "Recycle Bin cleaned up successfully."
}

# Call the function to clean up Recycle Bin
Clean-RecycleBin

Save this script with a .ps1 extension (e.g., Cleanup-RecycleBin.ps1). Then, you can schedule it to run at specified intervals using Task Scheduler in Windows.

Here's how you can schedule it:

1. Open Task Scheduler.

2. Click on "Create Basic Task" or "Create Task" from the right-hand pane.

3. Follow the wizard to specify a name and description for the task.

4. In the "Action" tab, choose "Start a program" and specify the path to PowerShell (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe).

5. In the "Add arguments" field, specify the path to the script you saved earlier (e.g., C:\Path\To\Script\Cleanup-RecycleBin.ps1).

5. Set the schedule for the task (daily, weekly, etc.).

6. Complete the wizard to create the task.

Thanks!!

No comments:

Post a Comment

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...