Downloads
125117.zip

Back in the 1990s, my first major Windows scripting project was automating a system cleanup and migration. One deceptively simple task was emptying the Recycle Bin. I finally ended up having a script delete the folder during the cleanup process.

Since then, tools like the Shell32.dll component have simplified working with special folders such as the Recycle Bin. For example, the VBScript script in Listing 1, EmptyRecycleBin.vbs, performs the task that I was trying to perform during the migration years ago—it empties the recycle bin without prompting. Like any Recycle Bin operation these days, this script has its limits: It only empties the current user's Recycle Bin, and if some of the items need elevated permissions to be deleted, you need to run the script with elevated permissions.

Listing 1: EmptyRecycleBin.vbs
 
Option Explicit
Dim sa, fso, item, items
Set sa = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Set items = sa.Namespace(10).Items()
On Error Resume Next
For Each item in items
  If fso.FileExists(item.Path) Then
    fso.DeleteFile item.Path, True
  Else
    fso.DeleteFolder item.Path, True
  End If
Next

While EmptyRecycleBin.vbs is useful for deleting all the items in the Recycle Bin, it doesn't let you delete individual items. That's where the PowerShell script Get-Recycled.ps1 comes in handy.

Get-Recycled.ps1 enumerates all items in the Recycle Bin. While enumerating these items, the script adds the following information:

  • The date the item was deleted (DeletionTime and DeletionTimeUtc)
  • The directory in which the item was originally located (OriginalParent)
  • The item's original name (OriginalName)
  • The item's complete original path (OriginalFullName)
  • How long the item has been in the Recycle Bin (Age)

You can then use this information to delete or perform another type of operation on specific Recycle Bin items. For example, Listing 2 shows some PowerShell commands that use this information to delete files that have been in the Recycle Bin more than 10 days, get the original location of all the files deleted in the last hour, and restore all the .zip files in the Recycle Bin.

Listing 2: Sample Commands That Use Get-Recycled.ps1

# Delete items that have been in the Recycle Bin
# more than 10 days.
Get-Recycled | ?\\{$_.Age.TotalDays -gt 10\\}
  | Remove-Item

# Get the original location of all items deleted
# in the last hour.
Get-Recycled | ?\\{$_.Age.TotalHours -le 1\\} |
  %\\{$_.OriginalFullName\\}

# Restore all .zip files without overwriting
# newer files with the same names.
Get-Recycled | ?\\{$_.Extension -eq ".zip"\\} |
  %\\{Move-Item $_ $_.OriginalFullName\\}

# Restore all .zip files, overwriting
# newer files if necessary.
Get-Recycled | ?\\{$_.Extension -eq ".zip"\\} |
  %\\{Move-Item $_ $_.OriginalFullName -Force\\}

You can download these sample commands, Get-Recycled.ps1, and EmptyRecycleBin.vbs by clicking the Download the Code Here button near the top of the page.