Windows Tip Sheet

Power Cleaning

You already have the most powerful tool for cleaning up old log files: PowerShell

In a column I did on compressing log files, there's a comment about the lack of good tools for cleaning up old log files. Well, this task is easier to accomplish than you think if you have Windows PowerShell. You can quickly delete old log files (or any file for that matter) with one long PowerShell expression, but for clarity I'm going to break it down into simple steps. All of the PowerShell expressions are written as a single line.

First, you need a variable to represent the cutoff date. Any files older than this date will be deleted. In PowerShell, type an expression like this:

[datetime]$dt="1/1/2006"

Or if you prefer something more dynamic, use an expression like this to get a date that is 30 days from today. Obviously, you should use a date range that meets your needs:

$dt=(Get-Date).AddDays(-30)

Next, as a sanity check, because you'll be deleting files, you want a report of what files are getting deleted:

Get-ChildItem "c:\LogFiles" | where {$_.LastWriteTime -le $dt} | Out-file c:\report.txt

You can use the Get-ChildItem cmdlet to list all files that meet your age requirement. You do this by piping the result of the command to the Where cmdlet. This cmdlet selects files where the LastWriteTime is less than or equal to the cutoff date. Finally, the expression sends the results to a text file for you to review.

Before I get to the actual deletion, let me point out that PowerShell has a nifty "WhatIf" feature that let's you test a command and see what the result will be:

Get-ChildItem "c:\LogFiles" | where {$_.LastWriteTime -le $dt} | remove-item -whatif

By now, you should be satisfied that the files to be deleted are OK and now you can run this command to do the dirty deed:

Get-ChildItem "c:\LogFiles" | where {$_.LastWriteTime -le $dt} | remove-item

Once you're comfortable, you can use a single line expression like this:

Get-ChildItem "c:\LogFiles" | where {$_.LastWriteTime -le (Get-Date).AddDays(-30)} | remove-item

If you need to fine-tune this, you can limit your search to a specific file type:

Get-ChildItem "c:\LogFiles" *.log | where {$_.LastWriteTime -le (Get-Date).AddDays(-30)} | |remove-item

You can also use -recurse if you need that feature, like this:

Get-ChildItem "c:\LogFiles" -recurse | where {$_.LastWriteTime -le (Get-Date).AddDays(-30)} | remove-item

Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the MCPmag.com editors at editor@mcpmag.com; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)

The beauty of this solution is that you could just as easily use Get-ChildItem on your local machine to clean up a remote server. The path can be a mapped drive or UNC.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.