Removing phantom application folders from website configuration in IIS

Thank you Ronald as I needed exactly this today. Here is how to remove phantom application folders from websites in IIS using PowerShell.

Today I wanted to remove application folders from the website configuration in IIS whose physical path on the filesystem was removed. E.g when the application folder “/test1” in the website example.com should point to z:\sites\example.com\www\test1 but was removed.

foreach( $server in (Get-ADComputer -Filter {(enabled -eq $True)} -SearchBase "OU=Webservers,$((Get-ADDomain).ComputersContainer)").DNSHostname) {
	Invoke-Command -ComputerName $server -ScriptBlock {
		Import-Module WebAdministration
		Write-Host $Using:server;
		Get-ChildItem IIS:\Sites | foreach {
			$site = $_;
			# Get all applications without existing physical path
			$applications = Get-ChildItem $site.PsPath | Where-Object { $_.NodeType -eq "application" -and (Test-Path $_.PhysicalPath) -eq $False };
			# List all phantom applications
			$applications | FT
			# Remove applications
			$applications | Remove-WebApplication -Site $site.Name
		}
	}
}Code language: PowerShell (powershell)

Source: https://serverfault.com/a/890561/161572.

Remember to Import-Module WebAdministration on Windows Server 2012 R2 and up instead of Add-PSSnapin WebAdministration. Get more Get-ADComputer examples from here.

This post showed you an easy way to remove orphaned IIS web applications for IIS administrators.

Jan Reilink

Hi, my name is Jan. I am not a hacker, coder, developer or guru. I am merely an application manager / systems administrator, doing my daily thing at Embrace - The Human Cloud. In the past I worked for clidn and Vevida. With over 20 years of experience, my specialties include Windows Server, IIS, Linux (CentOS, Debian), security, PHP, websites & optimization. I blog at https://www.saotn.org.