POWERSHELL: DELETE FILES OLDER THAN X DAYS EXCLUDE SOME FILES
POWERSHELL: DELETE FILES OLDER THAN X DAYS
This example will use PowerShell to delete files older than 90 days.
-Exclude a.jpg, a.png
# Delete all Files in C:\temp older than 90 day(s)
$Path = "C:\temp"
$Daysback = "-90"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Exclude a.jpg, a.png | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
If you need to delete files in subfolders too, you can use this script. This is the same script with the Get-Childitem parameter “-Recurse”.
# Delete all Files and sub folder files in C:\temp older than 90 day(s)
$Path = "C:\temp"
$Daysback = "-90"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse -Exclude a.jpg, a.png | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item
Comments
Post a Comment