Skip to content

Instantly share code, notes, and snippets.

@donnaken15
Created February 19, 2026 08:00
Show Gist options
  • Select an option

  • Save donnaken15/d7a0859401739c9cfb76c2f3d33ca1d1 to your computer and use it in GitHub Desktop.

Select an option

Save donnaken15/d7a0859401739c9cfb76c2f3d33ca1d1 to your computer and use it in GitHub Desktop.
attempt to backup recycle bin to a separate folder while keeping file info and unrecycled in tact (FAILING) | I HATE POWERSHELL, AND I HATE TECHNOLOGY!!
($repsych = (New-Object -ComObject Shell.Application).NameSpace(0x0a).Items() | Select-Object Name, Size, Path, IsLink, IsFolder, IsFileSystem, IsBrowsable, @{n="ModifyDate";e={$_.ModifyDate.ToString("yyyy-MM-ddTHH:mm:ssZ")}}, Type, @{n="OriginalLocation";e={$_.ExtendedProperty("System.Recycle.DeletedFrom")}}, @{n="DeleteDate";e={$_.ExtendedProperty("System.Recycle.DateDeleted").ToString("yyyy-MM-ddTHH:mm:ssZ")}}, @{n="Original";e={$_}}) | Select-Object -Property "*" -ExcludeProperty "Original" | ConvertTo-Json -Compress | Out-File -FilePath "C:/repsych.json" -Encoding utf8
$repsych | Select-Object -Property @{n="Root";e={$_.OriginalLocation}}, Name, Path, Original |
Select-Object -Property @{n="From";e={$_.Root+"\"+$_.Name}}, @{n="To";e={"$($_.Root[0]):\__REPSYCH\$($_.Root.Substring(3))"}}, * |
ForEach-Object {
$targ = $null
$FML = @($_.Root, $_.Name, [System.IO.Path]::GetFileNameWithoutExtension($_.Name), [System.IO.Path]::GetExtension($_.Name))
if (Test-Path "$($FML[0])\$($FML[1])") {
#echo already "$($FML[0])\$($FML[1])"
for ($i = 2; $i -le 99999; $i++) {
$KILLME = "$($FML[0])\$($FML[2]) ($($i))$($FML[3])"
if (-not (Test-Path $KILLME)) {
$targ = $KILLME
break
}
}
}
if (Test-Path $_.Path) {
$_.Original.InvokeVerb("undelete")
} else { return }
if (-not (Test-Path $_.From)) {
return
}
md $_.To -Force | Out-Null
$dest = ($why = $_).To
if ($targ -eq $null) {
$targ = $why.From
}
for ($i = 1; $i -le 1000; $i++) {
try {
mv -Path $targ -Destination $dest -ErrorAction Stop
echo "Restored $([System.IO.Path]::GetFileName($targ)) from $($_.Path)"
break
} catch {
if ($_.Exception.Message.Trim() -ceq "Cannot find path '$targ' because it does not exist.") {
Write-Host -ForegroundColor Red "Source file '$targ' doesn't exist, skipping."
break
}
if ($i -ge 1000) {
Write-Host -ForegroundColor Red "Failed to move copy ($($i)): ($($why.To))"
break
}
if ($_.Exception.Message.Trim() -ceq "Cannot create a file when that file already exists.") {
$dest = "$($why.Root)\$([System.IO.Path]::GetFileNameWithoutExtension($why.Name)) ($($i))$([System.IO.Path]::GetExtension($why.Name))"
continue
}
Write-Error $_
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment