ZIP old IIS Log files based on age

It can also remove the ZIPs too. . .

   Compresses IIS logs based on provided age in days
   Creates ZIP Archives in the default IIS log file path with a date code based on log file age adds those logs to the ZIP then removes the aged logs, also removes ZIP Archives based on a second parameter for ZIP file age in days.
   If you plan to run from a scheduled task un-remark the last line of the file and change the values to the desired number of days
   Zip-IISLogs -LogFileDays 30 -ZipFileDays 90
   Zip-IISLogs -LogFileDays <number of days old> -ZipFileDays <number of days old>
   If calling from the command line change the default values at the bottom of this script
function Zip-IISLogs
        # LogFileDays number of days old to archive and then delete

        # ZipFileDays number of days old to delete and remove the old ZIP files that are no longer needed

    $LogFileDays = (Get-Date).AddDays(-$LogFileDays)

    # Setup some global variables
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    $includeBaseDirectory = $false # if true creates a folder within a folder
    $defaultLogPath = Get-WebConfiguration -Filter system.applicationhost/sites/sitedefaults/logfile | Select-Object -ExpandProperty directory

    # TEMP foldername and filename variable
    $tempName = "IISLogs"+(Get-Date).ToShortDateString().Replace("/","-")
    $Destination = $defaultLogPath.Replace("%SystemDrive%",$env:SystemDrive)+"\"+$tempName+".zip"
    [System.IO.FileInfo]$tempFolder = $env:TEMP+"\"+$tempName
    New-Item -Path $tempFolder -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null

    function Get-IISLogFilePath
    # Load the PowerShell Module for IIS
    Import-Module WebAdministration

    $Sites = Get-ItemProperty 'IIS:\Sites\*'
    foreach ($Site in $Sites)
        $SiteID = $Site.ID
        $LogPath = $
        $Logpath = $LogPath.Replace("%SystemDrive%",$env:SystemDrive)
    $iisLogFolders = Get-IISLogFilePath
    foreach ($folder in $iisLogFolders)
        Get-ChildItem $folder -Recurse -Include *.log | Where-Object {$_.LastWriteTime -le $LogFileDays} | Move-Item -Destination $tempFolder -Force

    # Now create the zip file
    # Load the built in ZIP Assembly from Windows
    [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" )
    if ((Get-ChildItem $tempFolder).Length -lt 1) {Remove-Item $tempFolder -Recurse -Force -ErrorAction SilentlyContinue ; exit}
    Remove-Item $tempFolder -Recurse -Force -ErrorAction SilentlyContinue

    # Remove old ZIPs
    if ($ZipFileDays)
        Get-ChildItem -Path $defaultLogPath.Replace("%SystemDrive%",$env:SystemDrive) -Include *.zip -Recurse | Where-Object {$_.LastWriteTime -le (Get-Date).AddDays(-$ZipFileDays)} | ForEach-Object {Remove-Item $_.FullName -Force}
    (Get-ChildItem $Destination).FullName
# Remember to set your defaults below replace the variables with your numbers if you plan to run from a scheduled task then REMOVE the "#" from the last line
# HINT: ZipFileDays 0 will NOT delete old ZIP files 
# Zip-IISLogs -LogFileDays 30 -ZipFileDays 120

2 thoughts on “ZIP old IIS Log files based on age

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s