Count log files in folders then delete older than x days

Hi, I am trying to achieve the following:

  • Count the number of log files in several file paths then save the number to a txt file (along with date/time)
  • Delete log files older than 90 days
  • Count the number of files in the same folder then add this to the same log file
  • Ideally email the log file but haven’t got that far yet
$LogLocation1="C:\logs\Archive"
$LogLocation2="C:\logs\Processed\W3SVC1"
$LogLocation3="C:\logs\W3SVC1"

$TimeStamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")


$LogLocationALL=$LogLocation1,$LogLocation2,$LogLocation3

$LogWriteBefore = "$TimeStamp :: $i :: LOG File Count :: $CountBefore"
$LogWriteAfter = "$TimeStamp :: $i :: LOG File Count :: $CountAfter"


Foreach ($i in $LogLocationALL)

{
$countBefore = (Get-ChildItem –Path $i -Recurse | Where-Object {($_.Extension -match "log")}|Measure-Object).count
Add-Content -Path "C:\logs\counttestlog.txt" -Value $LogWriteBefore -PassThru
Get-ChildItem –Path $i -Recurse | Where-Object {($_.LastAccessTime -lt (Get-Date).AddDays(-90) -and $_.Extension -match "log")}|Remove-Item
$CountAfter = (Get-ChildItem –Path $i -Recurse | Where-Object {($_.Extension -match "log")}|Measure-Object).count
Add-Content -Path "C:\logs\counttestlog.txt" -Value $LogWriteAfter -PassThru
}

It’s crude but it works to some extent. My issue is that the log file only seems to be showing the last folder name and count. For example

PS C:\Users\demo> C:\Tools\scripts\DeleteLOG-OlderThan90DaysV2.ps1
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27
2021/11/12 10:29:08 :: C:\logs\W3SVC1 :: LOG File Count :: 27

I have really struggled in the past to make anything output to txt file in a presentable manner so I am sure there are better ways to go about this.

If anyone is able to help or point me in the right direction it would be much appreciated.
Thanks
BP

skuwlbp,
Welcome to the forum. :wave:t4:

You query each log folders 3 times. That’s unnecessary and time consuming. I’d do it this way:

$LogPathList = 'C:\logs\Archive', 'C:\logs\Processed\W3SVC1', 'C:\logs\W3SVC1'

$Result = 
Foreach ($LogPath in $LogPathList) {
    $LogFileList = Get-ChildItem –Path $LogPath -Filter *.log -Recurse
    $LogFileList | Where-Object { $_.LastAccessTime -lt (Get-Date).AddDays(-90) } | Remove-Item

    [PSCustomObject]@{
        LogPath     = $LogPath
        TimeStamp   = Get-Date -Format "yyyy/MM/dd HH:mm:ss"
        CountBefore = $LogFileList.count
        CountAfter  = ($LogFileList | Where-Object { $_.LastAccessTime -ge (Get-Date).AddDays(-90) }).count
    }
}
$Result 

Of course you can pipe the output to Export-Csv or send it by mail with Send-MailMessage.

1 Like

Olaf,
Thanks for the quick feedback and solution- it works as I need it to! I have also been able to export as CSV and attach via Send-MailMessage as suggested.

Cheers