Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anyone know how to use Powershell to create a 'Deny File Name Extension' rule, so that certain files will not be served by IIS?

For example, I want to set a rule to deny all .txt files from being served up with IIS.

thank you

share|improve this question
up vote 4 down vote accepted

How about this:

"Microsoft.Web.Administration, Version=, 
Culture=Neutral, PublicKeyToken=31bf3856ad364e35") > $null

$serverManager = New-Object Microsoft.Web.Administration.ServerManager

# Default Web Site
$siteId = 1
$site = $serverManager.Sites | where { $_.Id -eq $siteID }   

# Sets the filter in the site's web.config
$config = $site.GetWebConfiguration()
$requestFilteringSection = `

# Or delete last two lines and use next two instead to set in applicationHost.config
# $config = serverManager.GetApplicationHostConfiguration();
# $requestFilteringSection = `
    $config.GetSection("system.webServer/security/requestFiltering", $site.Name);

$fileExtensionsElement = `
$fileExtensionsCollection = $fileExtensionsElement.GetCollection()
$addElement = $fileExtensionsCollection.CreateElement("add")
$addElement["fileExtension"] = ".txt"
$addElement["allowed"] = $false


You could also use appcmd.exe:

Configure in site web.config:

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"fileExtensions.[fileExtension='.txt',allowed='False']" 

Or configure in applicationHost.config:

appcmd.exe set config "Default Web Site" -section:system.webServer/security/requestFiltering /+"fileExtensions.[fileExtension='.bong',allowed='False']" /commit:apphost
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.