Delete archived log files

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of John Provencher John Provencher 5 months, 1 week ago.

  • Author
    Posts
  • #44217
    Profile photo of Bob Withrow
    Bob Withrow
    Participant

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

  • #44223
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

    • #44371
      Profile photo of Bob Withrow
      Bob Withrow
      Participant

      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?

  • #44225
    Profile photo of Bob Withrow
    Bob Withrow
    Participant

    Much appreciated!!

  • #44477
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #44933
    Profile photo of John Provencher
    John Provencher
    Participant

    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.

You must be logged in to reply to this topic.