Remotely cleanup log and temp files from your Windows based servers with PowerShell

Setting up a scheduled task or job on individual servers to cleanup log and temp files just doesn't scale very well because you have scheduled tasks or jobs setup on lots of individual servers that need to be maintained. Today it's this server and tomorrow it's two or three more. It's much easier to setup one scheduled task or job on a server that's designed for management to remotely cleanup the desired files on all of your servers.

The following log and temp file cleanup script is provided as a proof of concept and should be thoroughly tested before running it against any production systems. You should also test your backups by performing a restore in a sandbox environment even once you've tested a script like this. In other words, always have a back-out plan.

This script can be setup as a scheduled task or job. It retrieves a list of server names from a text file located on a centralized file server. It adds different parameters based on if the path is for log or temp files. It also tests for the specified paths since not all servers will be web servers, but if the web server role is added to an existing server, those log files will be removed without any additional modifications. When new servers are added, simply add them to the text file. That could also be automated depending on the structure of your Active Directory environment by querying Active Directory for a list of server names.

 1Invoke-Command -ComputerName (Get-Content -Path '\\Server01\share\server-list.txt') {
 2    $Paths = "$env:SystemDrive\inetpub\logs", "$env:windir\System32\LogFiles", "$env:windir\Temp"
 3
 4    foreach ($Path in $Paths) {
 5        $Params = @{
 6            Path = $Path
 7            Recurse = $true
 8        }
 9
10        if ($Path -match 'log') {
11            $Params.Include = '*.log'
12            $Date = (Get-Date -OutVariable Now).AddDays(-90)
13        }
14        else {
15            $Date = (Get-Date -OutVariable Now).AddDays(-7)
16        }
17
18        if (Test-Path -Path $Path -PathType Container) {
19            Get-ChildItem @Params |
20            Where-Object {$_.CreationTime -le $Date} -OutVariable Results |
21            Remove-Item -Force -ErrorAction SilentlyContinue
22
23            $Results | Export-Csv -Path "$env:windir\Temp\files-removed-$($($Now).ToString('MMddyy')).csv" -Append -NoTypeInformation
24
25        }
26    }
27}

The results are stored in a CSV file located in the temp folder on the server where the files were removed from. The files containing the results are cleaned up along with the other temporary files based on the specified interval in the script.

µ