Skip to content

Instantly share code, notes, and snippets.

@davops
Created June 14, 2016 15:50
Show Gist options
  • Save davops/060db005e6fe7f9d5b25d16e527b4b0c to your computer and use it in GitHub Desktop.
Save davops/060db005e6fe7f9d5b25d16e527b4b0c to your computer and use it in GitHub Desktop.
Deleting sites and application pools from Microsoft IIS using PowerShell
#Deleting Sites and app pools in IIS 7 with PowerShell
$appCmd = "C:\windows\system32\inetsrv\appcmd.exe"
#lists all the sites
& $appcmd list site
#deletes a specific site
& $appcmd delete site "Name of site"
#deletes a specific application pool
& $appcmd delete apppool "Name of app pool"
#delete any app pools that use a certain username (in the identity column of the GUI)
$account = "TheDomain\TheAccount"
$AppPools = & $appcmd list apppool /processModel.userName:$account
foreach ($pool in $AppPools){
$pool = $pool.split(" ")[1] #get the name only
& $appcmd delete apppool $pool
}
#delete all app pools that use a certain username except for a given app pool name
$account = "TheDomain\TheAccount"
$AppPools = & $appcmd list apppool /processModel.userName:$account
foreach ($pool in $AppPools){
$pool = $pool.split(" ")[1] #get the name only
if ($pool -ne "MyAppPool")
{
& $appcmd delete apppool $pool
}
}
@gustinmi
Copy link

gustinmi commented Feb 24, 2021

$pool.split(" ")[1] is a bad choice if you have spaces in app pool names. Also i had to remove leading and trailing quotes from $pool or -ne was not ok. Thanks for the script though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment