Skip to content

Instantly share code, notes, and snippets.

@donnaken15
Last active April 18, 2026 20:39
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...probably) | 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={$_}}) | Out-Null #| 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 {
if (-not (Test-Path $_.Path)) { return }
#if (-not (Test-Path $_.From)) { return }
$targ = ($why = $_).To + "\" + $_.Name
$FML = @($_.To, $_.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)) {
Write-Host -ForegroundColor Yellow "Had to rename ($i)"
$targ = $KILLME
break
}
}
}
try {
md $_.To -Force | Out-Null # unc path creation not working
} catch {
return
}
for ($i = 1; $i -le 1000; $i++) {
try {
if ($true) {
$_.Original | mv -Destination $targ -ErrorAction Stop
}
echo "Restored $([System.IO.Path]::GetFileName($targ)) from $($_.Path)"
break
} catch {
$msg = $_.Exception.Message.Trim()
if ($msg -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 ($msg -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-Host -ForegroundColor Red $msg
Write-Host -ForegroundColor Red $why
Write-Host -ForegroundColor Red $targ
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment