clear
# Check For WebAministration - Start
try
{
$ImportSucceeded = import-module WebAdministration -ErrorAction Stop
}
catch
{
Write-Output "Unable to Import"
try
{
$AddSucceeded = add-pssnapin WebAdministration -ErrorAction Stop
}
catch
{
Write-Output "Unable to Add PS Snapin"
try
{
$AddFeatureSucceeded = Add-WindowsFeature WindowsPowerShellWebAccess -ErrorAction Stop
}
catch
{
Write-Output "Unable to Add PS Feature"
Write-Output $PSVersionTable
}
}
# Break
}
# Check For WebAministration - Stop
$appName = $args[0]
$physicalPath = "C:\inetpub\wwwroot\";
#Remove Site
Remove-Item IIS:\Sites\$appName -Force -Recurse
Remove-Item iis:\AppPools\$appName -Force -Recurse
Remove-Item $physicalPath$appName -Force -Recurse
clear
# Check For WebAministration - Start
try
{
$ImportSucceeded = import-module WebAdministration -ErrorAction Stop
}
catch
{
Write-Output "Unable to Import"
try
{
$AddSucceeded = add-pssnapin WebAdministration -ErrorAction Stop
}
catch
{
Write-Output "Unable to Add PS Snapin"
try
{
$AddFeatureSucceeded = Add-WindowsFeature WindowsPowerShellWebAccess -ErrorAction Stop
}
catch
{
Write-Output "Unable to Add PS Feature"
Write-Output $PSVersionTable
}
}
# Break
}
# Check For WebAministration - Stop
# Remove All IIS Sites - Start
$AppNames = (Get-ChildItem IIS:\Sites).name
# Remove Physical Files - Start
$srvmgr = new-object Microsoft.Web.Administration.ServerManager
foreach($site in $srvmgr.Sites | Sort-Object Name )
{
foreach($app in $site.Applications | Sort-Object Path )
{
foreach($virt in $app.VirtualDirectories | Sort-Object Path )
{
#write-Output $virt.PhysicalPath
if (Test-Path -LiteralPath $virt.PhysicalPath -PathType Container)
{
Remove-Item $virt.PhysicalPath -Force -Recurse
}
}
}
}
# Remove Physical Files - Stop
# Remove IIS Site Configurations - Start
foreach($appName in $AppNames)
{
#Remove Site
if (Test-Path IIS:\Sites\$appName -PathType Container)
{
Remove-Item IIS:\Sites\$appName -Force -Recurse
}
if (Test-Path IIS:\AppPools\$appName -PathType Container)
{
Remove-Item iis:\AppPools\$appName -Force -Recurse
}
}
# Remove IIS Site Configurations - Stop
# Remove All IIS Sites - Stop
Originally Posted on March 11, 2016
Last Updated on March 14, 2016
Last Updated on March 14, 2016
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.