2

Is there a way to empty the recycle bin using Powershell 2.0.

I do not want to update Powershell.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40

2 Answers2

2

You could clear recycle bin via com object. Like so:

$Shell= New-Object -ComObject Shell.Application 
$Bin = $Shell.NameSpace(10)
foreach ($Item in @($Bin.Items())){Remove-item $Item.Path -Force}
Kirill Pashkov
  • 3,118
  • 1
  • 15
  • 20
1

You could also directly call SHEmptyRecycleBin Win32 function:

$definition = @'
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
public static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, uint dwFlags);
'@
$winApi = Add-Type -MemberDefinition $definition -Name WinAPI -Namespace Extern -PassThru
$winApi::SHEmptyRecycleBin(0, $null, 7)

All recycle bins are deleted, no confirmation message is shown, no progress bar, no sound.

Paweł Dyl
  • 8,888
  • 1
  • 11
  • 27