I have written a small script to do some clean up on servers. You can choose how many days of logfiles to keep, which file types to delete and on which paths the script should operate.
Usage
.\LogCleanupScript.ps1 -Age 14 -FileTypes "*.tmp","*.txt" -Directories "C:\Windows\Temp","C:\Temp" -EnableLogging -Verbose
Script
<#
.SYNOPSIS
This Script is deleting files from specific directories.
.DESCRIPTION
This Script is deleting files from specific directories.
You can specify the age of the files to be deleted and choose file types and directories.
.PARAMETER Age
Specifies the age the file must have to be deleted.
.PARAMETER FileTypes
Specifies the file types to be deleted. Examples: *.* or *.log.
.PARAMETER Directories
Specifies the diectories in which files should be deleted.
.PARAMETER ReportMode
If this switch is set, the script runs only in report mode, not deleting any files. Use with -Verbose to get some output.
.PARAMETER EnableLogging
If this switch is set, A transcript of script execution will be created. Use with -Verbose to get some output.
.EXAMPLE
C:\PS> .\LogCleanupScript.ps1 -Age 14 -FileTypes "*.tmp","*.txt" -Directories "C:\Windows\Temp","C:\Temp"
.EXAMPLE
C:\PS> .\LogCleanupScript.ps1 -Age 14 -FileTypes "*.tmp","*.txt" -Directories "C:\Windows\Temp","C:\Temp" -ReportMode -Verbose
.EXAMPLE
C:\PS> .\LogCleanupScript.ps1 -Age 14 -FileTypes "*.tmp","*.txt" -Directories "C:\Windows\Temp","C:\Temp" -EnableLogging -Verbose
.LINK
https://techblog.ptschumi.ch
.NOTES
Author: Philippe Tschumi - https://techblog.ptschumi.ch
Last Edit: 2020-10-18
Version 2.0 - Rewritten
.INPUTS
None.
.OUTPUTS
None.
#>
[CmdletBinding(SupportsShouldProcess)]
Param(
[Parameter(Mandatory)]
[int]
[ValidateScript(
{
if(-not ($_ -gt 0)) {
throw "Negative values not allowed. Choose a number larger than 0."
}
return $_
}
)]
$Age,
[Parameter(Mandatory)]
[string[]]
[ValidateScript(
{
$_ | Foreach-Object {
if(-not ($_ -match '^\*\.(\*|[a-z]+)$')) {
throw "File Type '$_' is not specified corectly. Examples: *.* or *.log"
}
return $_
}
}
)]
$FileTypes,
[Parameter(Mandatory)]
[string[]]
[ValidateScript(
{
$_ | Foreach-Object {
if (-not (Test-Path $_)) {
throw "Path '$_' does not exist!"
}
return $_
}
}
)]
$Directories,
[switch]
$ReportMode,
[switch]
$EnableLogging
)
if($EnableLogging) {
Start-Transcript -Path "$($env:TEMP)\LogCleanupScript.log" -ErrorAction SilentlyContinue
Write-Verbose "Logging enabled."
}
Set-ExecutionPolicy Bypass
$LastWriteTime = (Get-Date).AddDays(-$Age)
Write-Verbose "Deleting files older than $($Age) in $($Directories.Count) directories ($($Directories -join ",")). File types to remove: $($FileTypes -join ",")"
if($ReportMode) {
Write-Warning "Running in Report only Mode. Not deleting any files."
}
$Files = @()
$Directories | ForEach-Object {
Write-Verbose "Processing directory $($_)..."
$Count = $Files.Count
$Files += Get-ChildItem $_ -Recurse -Include $FileTypes -File | Where {$_.LastWriteTime -le $LastWriteTime}
Write-Verbose "Found $($Files.Count - $Count) files."
}
Write-Verbose "Found $($Files.Count) files in Total."
$FilesProcessed = 0
$SuccessItems = 0
$FailedItems = 0
$Files | ForEach-Object {
try {
if(-not $ReportMode) {
Remove-Item $_ -Force -Confirm:$false -ErrorAction Stop
}
Write-Verbose "Deleting file $($_.FullName) was successful"
$SuccessItems++
}
catch {
Write-Warning "Deleting file $($_.FullName) failed with error $($_.Exception.Message)"
$FailedItems++
}
}
Write-Verbose "Processed $($Files.Count). Sucessfully deleted: $($SuccessItems). Failed items: $($FailedItems)."
if($EnableLogging) {
Stop-Transcript
}