0

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!

huck
  • 11
  • 1
  • 1
  • Possible duplicate of [Delete files older than 15 days using PowerShell](https://stackoverflow.com/questions/17829785/delete-files-older-than-15-days-using-powershell) – Thomas Rollet Nov 16 '17 at 07:43

2 Answers2

0
$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.

Vivek Kumar Singh
  • 3,223
  • 1
  • 14
  • 27
0

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.

Snak3d0c
  • 626
  • 4
  • 11
  • Yes, if the user is copying a file in any other folder then the Creation time will be modified to the time the user copied it. If the copy operation is done in the same folder as that of the existing file, then the newly copied file is renamed as `SomeFileName Copy`. `CreationTime` parameter might delete the newly copied file. In that case we can make use of the `LastWriteTime` parameter. See the changes in the answer. – Vivek Kumar Singh Nov 16 '17 at 09:50