SharePoint: PowerShell SPWebConfigModification

Below is some PowerShell to add a web.config modification using the SPWebConfigModification class.

Note that there are a couple of interesting things in this powershell

  1. Calling a generic method
  2. Using an enum

Note that the example below denies all users access to SharePoint. It should really be used in conjunction with another node that adds a user / group to give access to SharePoint.

Add-PSSnapin “Microsoft.SharePoint.Powershell”
[void][System.Reflection.Assembly]::Load(“Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c”)

$url = “http://localhost/”
$modName = “deny”
$modXPath = “configuration/system.web/authorization”
$modValue = “<deny users=`”*`”>”

$site = New-Object Microsoft.SharePoint.SPSite($url)
$webApp = $site.WebApplication;
$method = [Microsoft.Sharepoint.Administration.SPServiceCollection].GetMethod(“GetValue”, [Type]::EmptyTypes)
$closedMethod = $method.MakeGenericMethod([Microsoft.SharePoint.Administration.SPWebService])
$webService = $closedMethod.Invoke($webApp.Farm.Services, [Type]::EmptyTypes)

$mod = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification($modName, $modXPath)
$mod.Value = $modValue;
$mod.Type = [Microsoft.SharePoint.Administration.SPWebConfigModification+SPWebConfigModificationType]::EnsureChildNode
$mod.Sequence = 0


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>