{"id":77643,"date":"2016-03-30T12:15:12","date_gmt":"2016-03-30T19:15:12","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=77643"},"modified":"2019-02-18T09:10:52","modified_gmt":"2019-02-18T16:10:52","slug":"use-powershell-5-to-empty-the-recycle-bin","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-5-to-empty-the-recycle-bin\/","title":{"rendered":"Use PowerShell 5 to empty the recycle bin"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell 5.0 to empty the recycle bin on Windows 10<\/p>\n<p>&nbsp;<\/p>\n<p>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.<\/p>\n<p>But \u2026 and this is huge \u2026 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.<\/p>\n<p>It is time to shed some light on what I am calling the Windows PowerShell 5.0 stealth features.<\/p>\n<h2>A PowerShell cmdlet to empty the recycle bin<\/h2>\n<p>The recycle bin on Windows is awesome. It gives me a chance to say, \u201cwoops!\u201d 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 \u2013 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.<\/p>\n<p>In the past, I have written all kinds of scripts to do this \u2026 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.<\/p>\n<h2>Testing out the empty recycle bin cmdlet<\/h2>\n<p>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):<\/p>\n<p>1..25 | % {New-TemporaryFile }<\/p>\n<p>Now, I am going to look at the Temporary older to ensure that I have the temporary files there:<\/p>\n<p>explorer.exe $([io.path]::GetTempPath())<\/p>\n<p>Now I want to delete all of the files that are in my temporary folder:<\/p>\n<p>dir $([io.path]::GetTempPath()) | Remove-Item -Recurse<\/p>\n<p>Now it is time to see if they exist in the Recycle bin. So I use this bit of code to confirm:<\/p>\n<p>New-Object -ComObject Shell.Application).NameSpace(0x0a).Items() |<\/p>\n<p>Select-Object Name,Size,Path<\/p>\n<p>&nbsp;<\/p>\n<p>Ok, now I clear the recycle bin:<\/p>\n<p>Clear-RecycleBin -Force<\/p>\n<p>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:<\/p>\n<p>(New-Object -ComObject Shell.Application).NameSpace(0x0a).Items() |<\/p>\n<p>Select-Object Name,Size,Path<\/p>\n<p>&nbsp;<\/p>\n<p>Sweet! And no SendKeys was required.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. Also check out my <a href=\"https:\/\/blogs.technet.microsoft.com\/msoms\/\">Microsoft Operations Management Suite Blog<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell 5.0 to empty the recycle bin on Windows 10 &nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":599,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568],"tags":[609,3,608,45],"class_list":["post-77643","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","tag-powershell-5","tag-scripting-guy","tag-windows-10","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell 5.0 to empty the recycle bin on Windows 10 &nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/77643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/599"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=77643"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/77643\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=77643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=77643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=77643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}