IIS Script to disable directory browse for multiple applications

Viewed 149

This script below works wonderfully for one application/site if I know the hardcoded name. However, I am looking for a way to accomplish what this script does but for all sites under Default Website. Below is an example as well as shown in IIS. Any help is appreciated.

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/DEFAULT WEB SITE/TESTAPPLICATION2' -filter "system.webServer/directoryBrowse" -name "enabled" -value "False"

I am needed everything that is in bold underneath default website to be have directory browsing turned off. This would be simple if it was just one server, but we are looking at 50+ servers and a script would save a lot of time. Once again the application names are not hardcoded and are all different. I am just trying to figure out a way to disable the feature multiple sites/applications at a time.

  • Default Site

Test Application 1

Test Application 2

Test Application 3

1 Answers

This code should work:

$siteName = "Default Web Site"
$site = Get-IISSite -Name $siteName
$apps = $site.Applications
ForEach ($app In $apps) {
    Set-WebConfigurationProperty -PSPath "MACHINE/WEBROOT/APPHOST/$site$($app.Path)" -Filter "system.webServer/directoryBrowse" -Name "enabled" -Value "False"
}

What the script does:

  1. Get the site.
  2. Get the site's apps.
  3. Loop through each app and set the property you want.

If you want an one-liner, here it is:

$siteName = "Default Web Site"
(Get-IISSite -Name $siteName).Applications.foreach({Set-WebConfigurationProperty -PSPath "MACHINE/WEBROOT/APPHOST/$site$($_.Path)" -Filter "system.webServer/directoryBrowse" -Name "enabled" -Value "False"})
Related