Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
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