Hello there,
I am not sure what guidance you are in need, is the above script not working?
The following script will delete items in my Downloads directory that are older than 6 months:
$Folder = "G:\Downloads"
Delete files older than 6 months
Get-ChildItem $Folder -Recurse -Force -ea 0 |
? {!$.PsIsContainer -and $.LastWriteTime -lt (Get-Date).AddDays(-180)} |
ForEach-Object {
$_ | del -Force
$_.FullName | Out-File C:\log\deletedlog.txt -Append
}
Delete empty folders and subfolders
Get-ChildItem $Folder -Recurse -Force -ea 0 |
? {$.PsIsContainer -eq $True} |
? {$.getfiles().count -eq 0} |
ForEach-Object {
$_ | del -Force
$_.FullName | Out-File C:\log\deletedlog.txt -Append
}
PowerShell Basics: How to Delete Files Older Than X Days https://techcommunity.microsoft.com/t5/itops-talk-blog/powershell-basics-how-to-delete-files-older-than-x-days/ba-p/1255317
--If the reply is helpful, please Upvote and Accept it as an answer–