Script Cmdlet to Empty the Recycle bin
September 3, 2008 – 12:20 pm
This weekend I needed to delete some files so I could defrag my hard drive. I only had 2% free and needed to get past that 15% threshold so it would work efficiently. I proceeded to do some house cleaning and started deleting old unneeded files. When I was done I had to empty the recycle bin. Yes I know I could have just done Shift-Delete to permanently delete the files, but I usually don’t remember and this is a little safer in case I do delete something I need. So as with everything else I do, I immediately though how could I do this in PowerShell. Deleting the files was the easy part. Emptying the Recycle Bin, I had to research.
After some research I found out there was a Windows API function, SHEmptyRecycleBin, that does exactly what I needed. Of course, since it wasn’t a .Net function I wasn’t sure how I was going to use it in PowerShell. Then I realized that I had seen an example by jaykul where he imbedded C# code into a PowerShell script. He pretty much did all the work. I just tweaked it to fit my situation.
This script cmdlet will only work in the version 2 CTP, but if you are driven to get it to work in version 1 I think there are ways to get it done.










You must be logged in to post a comment.