Sunday, December 31, 2017

GPO for delete files under specific folder and empty recycle bin when user logoff and machine reboot

Empty Recycle Bin

If you are machine running on PowerShell 5.0, you can use “Clear-RecycleBin” cmdlet.

https://blogs.technet.microsoft.com/heyscriptingguy/2016/03/30/use-powershell-5-to-empty-the-recycle-bin/

Since Windows 7 native PowerShell is 2.0 only, we cannot use the PowerShell 5.0 cmdlet to clear recycle bin, we change to use “Clear-RecycleBinItems.ps1”  (https://thinkpowershell.com/powershell-script-empty-recycle-bin-logoff/)

clip_image001

Then, we create a GPO under User Configuration and apply to user OU

clip_image002

Since the PowerShell from GPO cannot run if we did not change Windows PowerShell Policy by the following GPO, the fast way is change the execution policy to “allow local scripts and remote signed scripts” and apply to machine OU

clip_image003

https://docs.splunk.com/Documentation/MSApp/1.4.2/MSInfra/ConfigurePowerShellExecutionpolicyinActiveDirectory

Delete files under specific folder when logoff

Batch file for delete file and GPO apply to user OU

clip_image004

Example:


:echo off

cmd.exe /c "echo Y|PowerShell.exe -NoProfile -Command Remove-item c:\Documents\*.* -Force"

cmd.exe /c "echo Y|PowerShell.exe -NoProfile -Command Remove-item c:\Documents\MYMUSI~1\*.* -Force"

cmd.exe /c "echo Y|PowerShell.exe -NoProfile -Command Remove-item c:\Documents\MYPICT~1\*.* -Force"

cmd.exe /c "echo Y|PowerShell.exe -NoProfile -Command Remove-item c:\Documents\MYVIDE~1\*.* -Force"

cmd.exe /c "echo Y|PowerShell.exe -NoProfile -Command Remove-item c:\Documents\download\*.* -Force"

exit


Reference:

https://thinkpowershell.com/powershell-script-empty-recycle-bin-logoff/

https://www.jaapbrasser.com/clear-recyclebin-new-powershell-5-0-cmdlet-available-in-windows-10/

http://baldwin-ps.blogspot.hk/2013/07/empty-recycle-bin-with-retention-time.html

https://4sysops.com/archives/powershell-versions-and-their-windows-version/ Print Friendly and PDF
Share/Bookmark

No comments:

Post a Comment