Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

Delete Folders Older Than

$
0
0

At one point this script worked for me to delete folders (backups) that were older than a set amount, replacing with the new back up. (omitted that part).

It’s been sitting unused, and now I need it. This portion isn’t working my my tests now. It will delete a newly created folder in c:test, but leave all of the old ones. Any clue what is wrong? Thanks

$limit = (Get-Date).AddDays(-7) $path = "C:test" # Delete files older than the $limit. Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force # Delete any empty directories left behind after deleting the old files. Get-ChildItem -Path $path -Recurse -Force | Where-Object { $_.PSIsContainer -and (Get-ChildItem -Path $_.FullName -Recurse -Force | Where-Object { !$_.PSIsContainer }) -eq $null } | Remove-Item -Force -Recurse 

submitted by /u/wogadmin
[link] [comments]

The post Delete Folders Older Than appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Latest Images

Trending Articles



Latest Images