Created
July 28, 2024 11:08
-
-
Save Joaquim3/728758db5c26df5ab831a96df9d9bfbf to your computer and use it in GitHub Desktop.
POWERSHELL ⇢ Delete all Files in specific folders older than 14 days
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #-------------------------------------------------------------------- | |
| # purpose : Delete all Files in specific folders older than 14 days | |
| # params : $sDirToCheck | |
| #-------------------------------------------------------------------- | |
| Function fncDeleteOldBackups([String] $sDirToCheck) | |
| { | |
| $Daysback = "-14" | |
| $CurrentDate = Get-Date | |
| $DatetoDelete = $CurrentDate.AddDays($Daysback) | |
| Get-ChildItem $sDirToCheck | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item | |
| } | |
| cls | |
| # For Powershell : just clic YES to ALL when prompted. | |
| #-------------------------------------------------------------------- | |
| # Disallow script restrictions policies | |
| #-------------------------------------------------------------------- | |
| #Set-Executionpolicy RemoteSigned | |
| #Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted" | |
| #example | |
| fncDeleteOldBackups ("c:\test") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment