Delete files older than… in powershell

I have a couple of directories that create files

Here’s a script I use for deleting files older than 30 days in powershell.

$Path = "C:\temp"
$Daysback = "-30"
 
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item

You need to chance the $Path location and if you want do delete files after another time frame you just have to edit the 30 to however many days you want.

Leave a Reply

Your email address will not be published. Required fields are marked *