Skip to content

Instantly share code, notes, and snippets.

@kou1okada
Last active May 21, 2025 09:55
Show Gist options
  • Save kou1okada/8223585156f221f7913a10570c934c98 to your computer and use it in GitHub Desktop.
Save kou1okada/8223585156f221f7913a10570c934c98 to your computer and use it in GitHub Desktop.
Get-RecycleBinFiles.ps1

Get-RecycleBinFiles.ps1

This script lists files in the $Recycle.Bin folder (known as "Recycle Bin") in TSV format.

Usage

# List files in the `$Recycle.Bin` to stdout
Get-RecycleBinFiles.ps1

# List files in the `$Recycle.Bin` and save to a file
Get-RecycleBinFiles.ps1 > $filepath  # Replace `$filepath` with your desired file path.
# Get-RecycleBinFiles
# Copyright (c) 2025 Koichi OKADA. All right reserved.
# This script is distributed under the MIT license.
$ssfBITBUCKET = 10
$Shell = New-Object -ComObject Shell.Application
$RecycleBinFolder = $Shell.NameSpace($ssfBITBUCKET)
if ($RecycleBinFolder -eq $null) {
Write-Host "Failed to open RecycleBin."
exit
}
if ($RecycleBinFolder.Items().Count -eq 0) {
Write-Host "RecycleBin is empty."
exit
}
function Get-RecycleBinFiles {
param (
[System.__ComObject]$Folder,
[System.String]$basepath = "")
$Folder.Items() |% {
$name = $_.Name
$path = $_.Path
$mtime = [System.DateTimeOffset]::new($_.ModifyDate , [System.TimeZoneInfo]::Local.BaseUtcOffset).LocalDateTime.ToString("yyyy-MM-dd HH:mm:ss")
$size = $_.Size
$dtime = [System.DateTimeOffset]::new($_.ExtendedProperty("DateDeleted"), [System.TimeZoneInfo]::Utc.BaseUtcOffset ).LocalDateTime.ToString("yyyy-MM-dd HH:mm:ss")
if ($basepath -eq "") {
$origpath = $Folder.GetDetailsOf($_, 1) + "\" + $name
$attr = $Folder.GetDetailsOf($_, 8)
} else {
$origpath = $basepath + "\" + $name
$attr = $Folder.GetDetailsOf($_, 6)
}
Write-Output "$path`t$basepath`t$origpath`t$attr`t$size`t$mtime`t$dtime"
if ($_.IsFolder -and ($attr -match "D")) {
Get-RecycleBinFiles $_.GetFolder $origpath
}
}
}
#$BOM = "$([System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::UTF8.GetPreamble()))"
Write-Output "${BOM}path`tbasepath`torigpath`tattr`tsize`tmtime`tdtime"
Get-RecycleBinFiles -Folder $RecycleBinFolder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment