Categories
PowerShell

Delete Folders Older Than # Days

PowerShell Script to delete folders older than # days

$Now=Get-Date
$Days="90"
$TargetFolder="C:\temp\"
$LastWrite=$Now.AddDays(-$Days)

$Folders = get-childitem -path $TargetFolder | 
Where {$_.psIsContainer -eq $true} | 
Where {$_.LastWriteTime -le "$LastWrite"} 

 foreach ($Folder in $Folders) {
 write-host "Now Deleting $Folder" -foregroundcolor "Green"
 Remove-Item $TargetFolder$Folder -recurse -Confirm:$false
 }