Use PowerShell 5 to empty the recycle bin


Summary: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell 5.0 to empty the recycle bin on Windows 10


It seems that now days when I talk to someone about Windows PowerShell 5.0 they get all excited and go on and on about Desired State Configuration (DSC). Now, I will agree, DSC is awesome. DSC rocks! Even on Windows PowerShell 4.0 DSC was great, but on Windows PowerShell 5.0 it is even better.

But … and this is huge … but, there is so much more in Windows PowerShell 5.0 than simply improvements to DSC. In fact, there is lots and lots of stuff in Windows PowerShell 5.0 that have been overshadowed by all the hoopla. AND, if you have Windows 10 (and more than 270,000,000 people are running Windows 10) then you already have Windows PowerShell 5.0.

It is time to shed some light on what I am calling the Windows PowerShell 5.0 stealth features.

A PowerShell cmdlet to empty the recycle bin

The recycle bin on Windows is awesome. It gives me a chance to say, “woops!” and then go get that file that I mis-moused. However, when the recycle bin takes up over a gigabyte of space on my hard drive, and when it contains tens of thousands of files, its uselessness quickly outweighs its utility. So, on a regular basis I empty the recycle bin – often when I am on a conference call that is dragging on, or when I am faced with writers block on some blog article. Needless to say, this is an ad hoc operation. I would like to do something more regular, so that when I need a deleted file, it is more easily found.

In the past, I have written all kinds of scripts to do this … including some in VBScript that even invoked SendKeys to deal with a user confirmation prompt that I could not get around. It worked, but it was a kludge. But now we have a way cool Windows PowerShell cmdlet to empty the recycle bin.

Testing out the empty recycle bin cmdlet

So I want to create some temporary files and then delete them. First I will create 25 temporary files (using the PowerShell 5 New-TemporaryFile cmdlet):

1..25 | % {New-TemporaryFile }

Now, I am going to look at the Temporary older to ensure that I have the temporary files there:

explorer.exe $([io.path]::GetTempPath())

Now I want to delete all of the files that are in my temporary folder:

dir $([io.path]::GetTempPath()) | Remove-Item -Recurse

Now it is time to see if they exist in the Recycle bin. So I use this bit of code to confirm:

New-Object -ComObject Shell.Application).NameSpace(0x0a).Items() |

Select-Object Name,Size,Path


Ok, now I clear the recycle bin:

Clear-RecycleBin -Force

Of course, I want to verify that the recycle bin is empty, so I use the following code to see that the recycle bin is empty:

(New-Object -ComObject Shell.Application).NameSpace(0x0a).Items() |

Select-Object Name,Size,Path


Sweet! And no SendKeys was required.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. Also check out my Microsoft Operations Management Suite Blog. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Discussion is closed.

Feedback usabilla icon