Delete folders older than X days using PowerShell

If you need to remove X days old folders from a command line here is a short PowerShell command which will take care of it.

PowerShell -Command "& Get-ChildItem "%1" | Where-Object {$_.PSIsContainer -and $_.LastWriteTime -le [System.DateTime]::Now.AddDays(-%2)} | Remove-Item -Recurse -Force"

Just replace %1 with a base folder and %2 with number of days.

One thought on “Delete folders older than X days using PowerShell

  1. Is there anyway to exclude a single folder from being deleted in the above script? Sorry but despite best efforts I really suck at PowerShell. 🙂

    Thanks,

    Joe

Leave a Reply

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