Last active
May 16, 2023 22:37
-
-
Save carlzoo/40b42ef26b9bdf2238ce52e6a0835dd2 to your computer and use it in GitHub Desktop.
Restore files in Recycling Bin by Deletion Date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
recovered some important documents for me