I'm deleting files manually on my system which are more than 6 months old, want to automate the process, is it possible via powershell? Pretty new to it!
I want to delete files more than 6 months old. Any help would be much invited!
I'm deleting files manually on my system which are more than 6 months old, want to automate the process, is it possible via powershell? Pretty new to it!
I want to delete files more than 6 months old. Any help would be much invited!
$ToDeleteLimit = (Get-Date).AddMonths(-6)
Get-ChildItem -Path \\PathToYourBackup -Recurse -Force | Where-Object { $_.LastWriteTime -lt $ToDeleteLimit } | Remove-Item -Force
Use the Recurse
parameter to iterate through the sub-directories and Force
parameter to delete hidden or read-only files.
So Vivek Kumar his line will work, but there is one important note, that why i'm posting it as an answer and not as a comment.
This will work but be careful, if you cut/paste an old file, the creationdate will be pasted as well. So say you cut and paste a file that has been on your harddrive for 2 years, your script will delete it on your first run (and not after your period of six months). If you COPY/paste it, then the creationdate will be the date on which you copy it.