I have had a few requests come in asking to create a job that will delete files, in a specific folder, that are older than 30 days. This was a relatively simple script to write, here it is:
$fileList = get-childitem -path <\\<server name>\<path>
foreach ($files in $filelist) {
if ($files.LastAccessTime.Date -lt (get-date).adddays(-30)) {
#write-host -ForegroundColor green $files.FullName
Remove-Item $files.FullName
} # end IF
} # end foreach
You can adjust the number of days by changing the (get-date).adddays(-30)) from -30 to whatever number of days you choose.
I have been able to use this cmdlet for many different jobs. That's what I like about Powershell...you can reuse code for different applications.
I hope you found this useful.
No comments:
Post a Comment