functions/function-Remove-Log.ps1

function Remove-LogFiles {
      <#
            .SYNOPSIS
            Remove outdated log files from a specific folder
            .DESCRIPTION
            Removes outdated log files from a specific folder that are older than X days
            .PARAMETER Path
            The path to where the log files are stored ($env:SystemDrive\MyLogs\)
            .PARAMETER Filter
            String to use for the log file name
            .PARAMETER Age
            # of days to retain. Anything older than the specified # of days will be removed
            .PARAMETER WriteLog
            Switch parameter to log what this function does to a file.
            To be used with the Write-Log Cmdlet from the module.
            .EXAMPLE
            Remove-LogFiles -Path $env:SystemDrive\MyLogs -Filter 'sample log' -Age 5
            .EXAMPLE
            Remove-LogFiles -Path "$env:ProgramData\My Log Folder\" -Filter * -Age 7 -WriteLog
            .EXAMPLE
            Remove-LogFiles -Path . -Filter * -Age 0
            Deletes all .log files in the current directory!
      #>

      param (
            [Parameter(Mandatory)][string]$Path,
            [Parameter(Mandatory)][string]$Filter,
            [Parameter(Mandatory)][ValidateRange(0, [int]::MaxValue)][int]$Age,
            [Parameter()][switch] $WriteLog = $false
      )
      
      $counter = 0

      if (!($Path.EndsWith('\'))){
            $Path = ($Path+'\')
      }

      if ($Filter.EndsWith('.log')){
            $Filter = $Filter.TrimEnd('.log')
      }
      
      If ((Test-Path $Path) -eq $false){
            switch ($WriteLog) {
                  $true {
                        Write-Log  -Message "[$($MyInvocation.MyCommand)] ERROR! The specified path [$Path] does not exist!"
                  }
                  $false {
                        Write-Host  -ForegroundColor Red "[$($MyInvocation.MyCommand)] " -NoNewline
                        Write-Host  "ERROR! The specified path [$Path] does not exist!"
                  }
            }
            Exit 1
      }

      $logFiles = Get-ChildItem -Path $Path -Filter "$Filter*.log" -File

      if ($null -eq $logFiles){
            switch ($WriteLog){
                  $true {
                        Write-Log -Message "[$($MyInvocation.MyCommand)] No log files found containing the string: $Filter"
                  }
                  $false {
                        Write-Host  -ForegroundColor Yellow "[$($MyInvocation.MyCommand)] " -NoNewline
                        Write-Host "No log files found containing the string: $Filter"
                  }
            }
            Exit 1
      }
      
      foreach ($file in $logFiles){
            if ($file.CreationTime -lt (get-date).AddDays(-1*$Age)){
                  $counter++
                  try {
                        Remove-Item -Path "$Path$($file.name)" -Force -Confirm:$false -ErrorAction Stop

                        switch ($WriteLog){
                              $true {
                                    Write-Log -Message "[$($MyInvocation.MyCommand)] Removed old log file: $($file.Name)"
                              }
                              $false {
                                    Write-Host  -ForegroundColor Green "[$($MyInvocation.MyCommand)] " -NoNewline
                                    Write-Host "Removed old log file: $($file.Name)"
                              }
                        }
                  }
                  catch {
                        switch ($WriteLog){
                              $true {
                                    Write-Log -Message "[$($MyInvocation.MyCommand)] Unable to remove old log file $($file.Name). Exception: $($_.Exception.Message)"
                              }
                              $false {
                                    Write-Host  -ForegroundColor Red "[$($MyInvocation.MyCommand)] " -NoNewline
                                    Write-Error -Message "Unable to remove old log file $($file.Name). Exception: $($_.Exception.Message)"
                              }
                        }
                        continue
                  }
            }
      }

      if ($counter -eq 0){
            switch ($WriteLog){
                  $true {
                        Write-Log -Message "[$($MyInvocation.MyCommand)] Found $($logFiles.Count) file(s) that cointained the string '$Filter' but was younger than the specified age of $Age day(s)."
                  }
                  $false {
                        Write-Host  -ForegroundColor Yellow "[$($MyInvocation.MyCommand)] " -NoNewline
                        Write-Host "Found $($logFiles.Count) file(s) that cointained the string '$Filter' but was younger than the specified age of $Age day(s)."
                  }
            }
      }
}