In PowerShell 5.0 a new cmdlet has been introduced that provides the ability to clear the Recycle Bin programmatically. It supports the following four parameters:
- DriveLetter
- Force
- WhatIf
- Confirm
Unfortunately there is no Get-RecycleBin cmdlet, for this purpose I have written a short function that enumerates the contents of Recycle Bin for the current user:
1 2 3 4 | Function Get-RecycleBin { (New-Object -ComObject Shell.Application).NameSpace(0x0a).Items() | Select-Object Name,Size,Path } |
By verifying this output we can now determine if we would like to Clear the Recycle Bin or if some files that are stored there should be recovered before deleting all files. The following command will delete all files from the Recycle Bin for all drive letters:
1 | Clear-RecycleBin |
The default mode of operation of this cmdlet is to ask for confirmation before deleting all files. This is similar behavior to right clicking the icon of Recycle Bin and clicking ‘Empty Recycle Bin’. To delete the files without confirmation the following code can be used:
1 | Clear-RecycleBin -Force |
Alternatively the Confirm parameter can also be explicitly set to $false:
1 | Clear-RecycleBin -Confirm:$false |
Unfortunately there is no online help available for this cmdlet, so for more information about this cmdlet please refer to the built-in help in PowerShell:
1 | Get-Help Clear-RecycleBin -ShowWindow |
For more information or the direct download links of these scripts please refer to the links below. Feel free to leave a comment either here or in the TechNet Script Library.
TechNet Script Gallery |
My entries in TechNet Script Gallery |
Get-RecycleBin – shows the contents of the Recycle Bin |
Pingback: Get-RecycleBin function | Jaap Brasser's Blog
Very nice Cmdlet. Is it possible to create a clear-recyclebin for earlier versions of powershell using similar method?
Absolutely, have a look at this example by Rich Prescott:
Empty-RecycleBin
You can use that script on older versions of Windows as well.