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.

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.


Leave a Reply

%d bloggers like this: