How to Make changes to Web.config in SharePoint 2013/2010 using PowerShell

Hello Share Pointers,

In one of my client requirement, I have been asked to change Web.config using Powershell. This situation arises for many of us where you are not supposed to change web.config directly because it contains settings for entire web application. if anything goes wrong, its difficult to bring back the environment to a stable state. So we should prefer Powershell to make changes to Web.Config file in SharePoint environment.

Here is a sample Powershell scripts given below to make mobile view  available for the SharePoint environment. ISMobiledevice is a property that we need to take a look at it.

 

$webApp = Get-SPWebApplication http://mysite
$config = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$config.Path = “configuration/system.web”
$config.Name = “browserCaps”
$config.Sequence = 0
$config.Type = 0
$config.Value = “<browserCaps> </browserCaps>”

$configinto = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$configinto.Path = “configuration/system.web/browserCaps”
$configinto.Name = “result”
$configinto.Sequence = 0
$configinto.Type = 0
$configinto.Value = “<result type=””System.Web.Mobile.MobileCapabilities, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a””/><filter>isMobileDevice=True</filter>”

$webApp.WebConfigModifications.Add($config)
$webApp.WebConfigModifications.Add($configinto)
$webapp.Update()
$webapp.Parent.ApplyWebConfigModifications()
$webapp.WebConfigModifications.Clear()

Happy SharePointing 🙂

Leave a Reply

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