Script to clean out log files, or any files in a directory based on date

​$fileage = 30

$files = Get-ChildItem 'C:\PathToFiles\subdirectory' -Recurse -Include *.log

$files += Get-ChildItem 'E:\AnotherDriveAndPath\Subdirectory' -Recurse -Include *.log

ForEach ($File in $Files){

IF(((Get-Date) - $File.lastwritetime).Days -ge $fileage) {

Remove-Item $File

}

}

 

 

If you only want one week, change the file age to 7. Or, if you want 90 days, then change it to 90.

The paths to clean specified in this example are meant to be changes and you dont have to use subdirectories, and\or you could use more subdirectories.

**Works great for cleaning out log directories for IIS

Cheers,