Delete archived log files

Welcome Forums General PowerShell Q&A Delete archived log files

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
2 years, 9 months ago.

  • Author
    Posts
  • #44217

    Participant
    Points: 0
    Rank: Member

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

  • #44223

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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

      Participant
      Points: 0
      Rank: Member

      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

    Participant
    Points: 0
    Rank: Member

    Much appreciated!!

  • #44477

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Points: 0
    Rank: Member

    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.

The topic ‘Delete archived log files’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort