Skip to content

Instantly share code, notes, and snippets.

@perXautomatik
Forked from carlzoo/restore.ps1
Created May 16, 2023 22:37
Show Gist options
  • Save perXautomatik/0425119866f803461a415fb909efb3b0 to your computer and use it in GitHub Desktop.
Save perXautomatik/0425119866f803461a415fb909efb3b0 to your computer and use it in GitHub Desktop.
Restore files in Recycling Bin by Deletion Date
#https://stackoverflow.com/questions/52031528/powershell-get-deleted-files-of-a-folder
#https://jdhitsolutions.com/blog/powershell/7024/managing-the-recycle-bin-with-powershell/
#https://stackoverflow.com/questions/16906170/create-directory-if-it-does-not-exist
New-Variable -Name 'ssfBITBUCKET' -Option Constant -Value 0x0A;
New-Variable -Name 'BitBucketDetails_Name' -Option Constant -Value 0;
New-Variable -Name 'BitBucketDetails_ParentPath' -Option Constant -Value 1;
New-Variable -Name 'BitBucketDetails_DeletionTimeText' -Option Constant -Value 2;
New-Variable -Name 'BitBucketDetails_SizeText' -Option Constant -Value 3;
New-Variable -Name 'BitBucketDetails_Type' -Option Constant -Value 4;
New-Variable -Name 'BitBucketDetails_LastWriteTimeText' -Option Constant -Value 5;
New-Variable -Name 'BitBucketDetails_CreationTimeText' -Option Constant -Value 6;
New-Variable -Name 'BitBucketDetails_LastAccessTimeText'-Option Constant -Value 7;
New-Variable -Name 'BitBucketDetails_AttributesText' -Option Constant -Value 8;
$application = New-Object -ComObject 'Shell.Application';
$bitBucket = $application.NameSpace($ssfBITBUCKET);
foreach ($deletedItem in $bitBucket.Items())
{
$file = New-Object -TypeName 'PSObject' -Property @{
# Same as $deletedItem.Name
Name = $bitBucket.GetDetailsOf($deletedItem, $BitBucketDetails_Name);
ParentPath = $bitBucket.GetDetailsOf($deletedItem, $BitBucketDetails_ParentPath);
DeletionTimeText = $bitBucket.GetDetailsOf($deletedItem, $BitBucketDetails_DeletionTimeText);
Size = $deletedItem.Size;
SizeText = $bitBucket.GetDetailsOf($deletedItem, $BitBucketDetails_SizeText);
# Same as $deletedItem.Type
Type = $bitBucket.GetDetailsOf($deletedItem, $BitBucketDetails_Type);
LastWriteTime = $deletedItem.ModifyDate;
LastWriteTimeText = $bitBucket.GetDetailsOf($deletedItem, $BitBucketDetails_LastWriteTimeText);
CreationTimeText = $bitBucket.GetDetailsOf($deletedItem, $BitBucketDetails_CreationTimeText);
LastAccessTimeText = $bitBucket.GetDetailsOf($deletedItem, $BitBucketDetails_LastAccessTimeText);
AttributesText = $bitBucket.GetDetailsOf($deletedItem, $BitBucketDetails_AttributesText);
IsFolder = $deletedItem.IsFolder();
BitBucketPath = $deletedItem.Path;
};
$month = '10';
$date = '2';
$year = '2021';
if ($file.DeletionTimeText.Contains($month) -and $file.DeletionTimeText.Contains($date) -and $file.DeletionTimeText.Contains($year)) #full date string matching breaks due to unicode in the timestamp
{
$destinationFullPath=-join($file.ParentPath,'\',$file.Name)
$realFullPath=-join($file.ParentPath,'\',$file.Name)
Write-Output "Processing: $realFullPath"
if(!(test-path $file.ParentPath))
{
New-Item -ItemType Directory -Force -Path $file.ParentPath
}
Move-Item -Path $file.BitBucketPath -Destination $destinationFullPath
} else
{
Write-Output "not the right one" $file.DeletionTimeText
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment