Hi,
I try to elegantely remove old files and folders.
I got this Script Snippet:
Function RemoveOldFiles
{
$TimeSpan = "14"
$TargetFolder = "D:\test\folder"
if (Test-Path $TargetFolder)
{
$Now = Get-Date
$LastWrite = $Now.AddDays(-$TimeSpan)
$Files = Get-Childitem $TargetFolder -include * -recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{
write-host "Deleting file $File";
Remove-Item $File | out-null
}
}
else
{
Write-Host "'$TargetFolder' doesn't exist." -foregroundcolor "Red"
}
}
RemoveOldFilesBut I also want to delet the now empty directories ...
Is there a way to easily change the existing snippet