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

Popular posts from this blog

Extracting and using a modified VMWare Player BIOS or UEFI firmware

Activate Synology Active Backup for Business for DSM 6

Windows 11 internet/account bypass