Help others and share!

I recently had a request to empty the Windows Recycle Bin on logoff. I had hoped to find an official Microsoft GPO that would support a policy of emptying the trash can upon logoff. No such policy existed. The next best thing in terms of GPO was to enable the policy “Do not move deleted files to the Recycle Bin”. We didn’t want to go down this route. Although much more secure, it doesn’t afford the users any ability to recover from an accidental deletion.

After a bit of research, I came across this PowerShell script, which worked very well for my needs. For implementation, I added the code as a log off action item in AppSense.

The code itself comes from the TechNet Script Center, thanks to Windows Engineer and PowerShell Blogger Rich Prescott.

$Shell = New-Object -ComObject Shell.Application 
$RecBin = $Shell.Namespace(0xA) 
$RecBin.Items() | %{Remove-Item $_.Path -Recurse -Confirm:$false}

This script allows you to view the contents of the recycle bin in your profile. The first line creates a ComObject and then the second line grabs the Recycling Bin special folder. It then enumerates the items contained in that special folder and removes each of them. The Remove-Item cmdlet includes a switch to turn off confirmation for the removal of the files. It can be removed if you would like to be prompted for each file.

Works on:

Windows Server 2012 No
Windows Server 2008 R2 No
Windows Server 2008 No
Windows Server 2003 No
Windows 8 Yes
Windows 7 Yes
Windows Vista Yes
Windows XP Yes
Windows 2000 No

SOURCE

Help others and share!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.