I know I'm a little late to the party, but I thought I might contribute my subjectively more graceful solution.
I was looking for a script that would empty the Recycle Bin with an API call, rather than crudely deleting all files and folders from the filesystem. Having failed in my attempts to RecycleBinObject.InvokeVerb("Empty Recycle &Bin")
(which apparently only works in XP or older), I stumbled upon discussions of using a function embedded in shell32.dll called SHEmptyRecycleBin()
from a compiled language. I thought, hey, I can do that in PowerShell and wrap it in a batch script hybrid.
Save this with a .bat extension and run it to empty your Recycle Bin. Run it with a /y
switch to skip the confirmation.
<# : batch portion (begins PowerShell multi-line comment block)
:: empty.bat -- http://stackoverflow.com/a/41195176/1683264
@echo off & setlocal
if /i "%~1"=="/y" goto empty
choice /n /m "Are you sure you want to empty the Recycle Bin? [y/n] "
if not errorlevel 2 goto empty
goto :EOF
:empty
powershell -noprofile "iex (${%~f0} | out-string)" && (
echo Recycle Bin successfully emptied.
)
goto :EOF
: end batch / begin PowerShell chimera #>
Add-Type shell32 @'
[DllImport("shell32.dll")]
public static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath,
int dwFlags);
'@ -Namespace System
$SHERB_NOCONFIRMATION = 0x1
$SHERB_NOPROGRESSUI = 0x2
$SHERB_NOSOUND = 0x4
$dwFlags = $SHERB_NOCONFIRMATION
$res = [shell32]::SHEmptyRecycleBin([IntPtr]::Zero, $null, $dwFlags)
if ($res) { "Error 0x{0:x8}: {1}" -f $res,`
(New-Object ComponentModel.Win32Exception($res)).Message }
exit $res
Here's a more complex version which first invokes SHQueryRecycleBin()
to determine whether the bin is already empty prior to invoking SHEmptyRecycleBin()
. For this one, I got rid of the choice
confirmation and /y
switch.
<# : batch portion (begins PowerShell multi-line comment block)
:: empty.bat -- http://stackoverflow.com/a/41195176/1683264
@echo off & setlocal
powershell -noprofile "iex (${%~f0} | out-string)"
goto :EOF
: end batch / begin PowerShell chimera #>
Add-Type @'
using System;
using System.Runtime.InteropServices;
namespace shell32 {
public struct SHQUERYRBINFO {
public Int32 cbSize; public UInt64 i64Size; public UInt64 i64NumItems;
};
public static class dll {
[DllImport("shell32.dll")]
public static extern int SHQueryRecycleBin(string pszRootPath,
out SHQUERYRBINFO pSHQueryRBInfo);
[DllImport("shell32.dll")]
public static extern int SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath,
int dwFlags);
}
}
'@
$rb = new-object shell32.SHQUERYRBINFO
# for Win 10 / PowerShell v5
try { $rb.cbSize = [Runtime.InteropServices.Marshal]::SizeOf($rb) }
# for Win 7 / PowerShell v2
catch { $rb.cbSize = [Runtime.InteropServices.Marshal]::SizeOf($rb.GetType()) }
[void][shell32.dll]::SHQueryRecycleBin($null, [ref]$rb)
"Current size of Recycle Bin: {0:N0} bytes" -f $rb.i64Size
"Recycle Bin contains {0:N0} item{1}." -f $rb.i64NumItems, ("s" * ($rb.i64NumItems -ne 1))
if (-not $rb.i64NumItems) { exit 0 }
$dwFlags = @{
"SHERB_NOCONFIRMATION" = 0x1
"SHERB_NOPROGRESSUI" = 0x2
"SHERB_NOSOUND" = 0x4
}
$flags = $dwFlags.SHERB_NOCONFIRMATION
$res = [shell32.dll]::SHEmptyRecycleBin([IntPtr]::Zero, $null, $flags)
if ($res) {
write-host -f yellow ("Error 0x{0:x8}: {1}" -f $res,`
(New-Object ComponentModel.Win32Exception($res)).Message)
} else {
write-host "Recycle Bin successfully emptied." -f green
}
exit $res