cleanup old logs, 1 line script in PowerShell

I was seeking for simple ways to cleanup old logs… google does pull out a lot of stuff, there were plenty of WMI/VBS/.cmd scripts…. however, they were so huge too… I wasn’t really convinced to go for 25+ lines of script just to clean up some log files on regular basis… all I wanted a simple, few lines (not more than 5 lines) of script…..
PowerShell is so sweet… here we go… 3 lines…

$a = get-Date
$a = $a.addDays(-15)
dir <path> | Where-Object { $_.LastWriteTime -lt $a } | remove-item -recurse -force

Upon scheduling this script, it’ll retain only last 15 days of file….delete all old (golden) junks…. 😉

I just enhanced it with a foreach to iterate list of files & delete them…. enjoy guys… PowerShell  – all good and powerful stuff…. nice job PowerShell team… this really makes windows simple …


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s