Delete archived log files

I’m new to PowerShell but how would I delete all archived log files older than 365 days on Windows Server 2008 R2?

I’ll get you started.

Get-ChildItem | Where-Object | Remove-Item

Use the first command (you’ll obviously need to specify a starting path and other parameters) to get the candidate files.

Use the second command (again, you’ll need to add to it) to speicfy your criteria. For example…

Where-Object { $_.LastAccessTime -lt ((Get-Date).AddDays(-365)) }

Or something like that. I’m not sure exactly what file date/time you’re concerned with. Read the help on those commands, look at examples in the help files (Help Get-ChildItem -Full), and consider searching these forums. For example, searching on “AddDays” will probably turn up other people who’ve been doing similar things.

Much appreciated!!

Thanks Don. I was able to complete the above after adding some variables. Now I have to: Append file names for all files deleted, with a time date stamp, to a log file in the same directory.

Any suggestions or directions on accomplishing this?

Get-ChildItem | Where-Object | foreach {
remove-item $_
“{0} {1}” -f $_.fullname,get-date | out-file whatever.txt -append

I’ll also offer a plug for “Learn Windows PowerShell in a Month of Lunches.” It’ll help you construct these kinds of things on your own a bit more easily.

I have a script that collects the archived logs from all servers and stores them on the network. With the amount we are required to audit, servers’ hard drives would fill within a day or two. Any of the logs over 30 days old in network storage are 7zipped. I use the filename for date calculations because we’ve ended up with new files dated from 1980.