Halloween is upon us and the Chairman has devised a demonic PowerShell scripting challenge for you. This is not for the faint of heart. The Chairman wants you to raise the dead. Specifically, deleted items in your Recycle Bin on a Windows 10 desktop. There are several parts to this challenge.
First, calculate how much space is being used by files in the recycle bin. At least based on the detected file size. This value may be different than what Windows reports in Settings or with other tools. And if you have multiple drives don’t forget to take that into account. The second part of the challenge is to write PowerShell code to move a file from the recycle bin back to its original location and name.
You are welcome to use any approach you’d like. The Recycle Bin can be accessed as a hidden directory like C:\$Recycle.Bin but it’s behavior is a bit different than a typical file system folder. Still, that is a possibility for the first part of the challenge. However, for the second part of the challenge you will need to discover the original filename and location.
One possible approach is to use the Shell.Application COM object.
$shell = New-Object -com shell.application $rb = $shell.Namespace(10) $rb.items()
Of course, you’ll need to take deleted folder structures into account.
You are welcome to use any approach you want. The Shell.Application is merely a suggestion for people who have no idea on where to even begin.
As always, please don’t submit any solutions in the comments. Links to your work are always welcome. Halloween is almost upon us and the dead won’t wait so you’d better get to it!
I like this challenge and it was definitely challenging.
I created two functions, Get-RecycleBin and Restore-RecycleBinItem, that should satisfy each part of the challenge, respectively. I threw in validation for both and confirmation prompt for restores for good measure.
https://gist.github.com/thedavecarroll/fa0507331a664ac6dfa464b40b2511e1
Please feel free to post any comments on the gist.
I plan on writing a few other functions for searching the recycle bin, sending items to the recycle bin, and for permanently deleting individual items from the recycle bin.
I’ll write a blog post on all of these when I’m done.
Did you test with deleted folders that include files? Your version only gives me 122 files in my recycle bin. The code I’ve been testing with gave me over 1700. I don’t think you are enumerating the subfolders.
You are correct – my code does not enumerate items in a folder. That was a ghastly oversight on my part.
My usage output shows large folder sizes based on the file contents being included in the size of the folder. And when I restore a folder containing files, it restores the entire folder.
I did say it was challenging. 🙂
Looks like a revision is in order. I may not be able to get back to this until after the weekend, though.
Here’s some code that I came up with for this challenge: https://jdhitsolutions.com/blog/powershell/7024/managing-the-recycle-bin-with-powershell/