Skip to content

Clear-RecycleBinRemote

Windows: Clears the recycle bin for one or more drives

#Requires -Version 5.1

[CmdletBinding()]
Param
(
    [string]$ComputerName = $env:COMPUTERNAME,

    [string[]]$DriveLetter,

    [switch]$Force,

    [pscredential]$Credential
)

Process
{
    try
    {
        $clearParams = @{
            'Confirm'     = $false
            'ErrorAction' = 'Stop'
        }

        if ($Force)
        {
            $clearParams.Add('Force', $true)
        }

        if ($null -ne $DriveLetter)
        {
            $clearParams.Add('DriveLetter', $DriveLetter)
        }

        if ($ComputerName -ne $env:COMPUTERNAME)
        {
            $invokeParams = @{
                'ComputerName' = $ComputerName
                'ScriptBlock'  = {
                    Param($Params)
                    Clear-RecycleBin @Params
                }
                'ArgumentList' = $clearParams
                'ErrorAction'  = 'Stop'
            }
            if ($null -ne $Credential)
            {
                $invokeParams.Add('Credential', $Credential)
            }

            Invoke-Command @invokeParams
        }
        else
        {
            Clear-RecycleBin @clearParams
        }

        $result = [PSCustomObject]@{
            ComputerName = $ComputerName
            Action       = "Recycle bin cleared"
            Drives       = if ($null -ne $DriveLetter) { $DriveLetter -join ', ' } else { "All" }
            Timestamp    = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch
    {
        throw
    }
}

Specifies the name of the target computer. Defaults to the local computer.

Specifies one or more drive letters (e.g., "C", "D") to clear. If omitted, all recycle bins are cleared.

Off

If set, clears the recycle bin without prompting for confirmation.

Specifies a PSCredential object for remote connection.

An interactive directory of PowerShell scripts.