Skip to content

Instantly share code, notes, and snippets.

@kkamegawa
Last active October 14, 2015 01:18
Show Gist options
  • Save kkamegawa/4285724 to your computer and use it in GitHub Desktop.
Save kkamegawa/4285724 to your computer and use it in GitHub Desktop.
Remove IIS Log file for IIS 7.0/7.5/8.0 after 7 days.
import-module WebAdministration
$iisroot = 'IIS:\Sites\'
$sites = get-itemproperty $iisroot
foreach($site in $sites.children) {
foreach($webroot in $sites.Children.Keys) {
$WebSite = join-path $iisroot $webroot
$logpath = get-itemproperty $WebSite -name logfile.directory.value
if($logpath -ne $null) {
$logpath = [environment]::ExpandEnvironmentVariables($logpath)
$files = get-childitem $logpath -include "*.log" -recurse
foreach($file in $files) {
if($file.lastwritetime.adddays(7) -lt [Datetime]::Now) {
remove-item $file.fullname
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment