Modify IIS Site Log Location With PowerShell

Posted by AgileCoder on February 22, 2021

Over the years I have inherited multiple projects and shared web servers as I have moved teams. Often these servers have been setup or configured by several different people now long gone, and many times the defaults in IIS have never been changed. That can be a problem.

Most recently I got a frantic call from a co-worker saying “all the sites on one of the production servers have quit working.” Scrambling, I logged in with Remote Desktop to check it out. Sure enough, this server (a VM actually) has a typical setup of a small OS drive partition (C:) and a much larger Data drive partition (E:). The C: partition was reporting only double digit megabytes of free space.

Every Windows box I have dealt with in the past had an environment variable set for %SystemDrive% and that has always pointed at the C: drive in my experience. So I checked the default location for IIS logs %SystemDrive%\inetpub\logs\LogFiles and found that it contained over 120 GB of log data.

Now, there are several issues that could be discussed, like monitoring so this problem was caught 30 or 40 GB earlier. Or like log storage, mining and truncation strategies. But, the immediate problem was that I have a Data drive partition that has nearly 1.5 TB of space, and I need to configure IIS to put the logs on that drive.

I manually changed one of the sites log locations using IIS, and then realized that not only do I have 41 sites on this shared server, I also have a TEST/QA server with the same structure.

Time for PowerShell.

After a couple of tests, I arrived at the following script, saved as switch-log-location.ps1.

Import-Module WebAdministration

# Set your desired root here... 	
$LogPath = "E:\IIS_Logs"

foreach($site in (ls iis:\sites\*))
	$PathExists = Test-Path -Path $LogPath\$($site.Name)
	if(-Not $PathExists)
		New-Item $LogPath\$($site.Name) -type directory
	Set-ItemProperty IIS:\Sites\$($site.Name) -name -value "$LogPath\$($site.Name)"

In seconds, all of the sites will have their log location updated, and you can get started on cleaning up the cruft in the old location. Even better, the new location will have a folder name that matches the site name for every site on the shared server.