Skip to content

Instantly share code, notes, and snippets.

@Joaquim3
Created July 28, 2024 11:08
Show Gist options
  • Select an option

  • Save Joaquim3/728758db5c26df5ab831a96df9d9bfbf to your computer and use it in GitHub Desktop.

Select an option

Save Joaquim3/728758db5c26df5ab831a96df9d9bfbf to your computer and use it in GitHub Desktop.
POWERSHELL ⇢ Delete all Files in specific folders older than 14 days
#--------------------------------------------------------------------
# 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