How to remove a node from web.config file of a web application (PowerShell)
Introduction
The goal of this script is to remove a node from web.config file of a web application.
Scenarios
The script will help to remove a node in web.config file of a web application in SharePoint.
Script
Step 1: Run PowerShell with administrator.
Step 2: Drag the script to PowerShell console and input arguments as the following.
Note:
1. The argument Path is case-sensitive.
2.The argument Path format should like //Rootnode/childnode[@attribute = 'value']/ ,for example:
Path: "//configuration/configSections/sectionGroup[@name = 'SharePoint']/section[@name = 'WebPartLimits']"
Content in web.config:
Here are some code snippets for your references:
PowerShellEdit|RemovepowershellAdd-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue $web = Get-SPWebApplication -Identity $Webapplication $zone = $web.AlternateUrls[0].UrlZone $iissetting = $web.IisSettings[$zone] $webPath = $iissetting.Path.ToString() + "\web.config" $webcontent = [XML](Get-Content -Path $webPath) $node = $Webcontent.SelectSingleNode($path) If($node) { try { $node.Removeall() $date = Get-Date -Format o $datetime = $date.Substring(0,$date.IndexOf(".")) $backtime = ($datetime -replace "-","") -replace ":","" $destination = $iissetting.Path.ToString() + "\web"+$backtime+"backup.config" Copy-Item -Path $webpath -Destination $destination write-host "Backup web.config to $destination" -ForegroundColor Green $webcontent.save($webpath) write-host "Remove node '$path' from web.config of $webapplication successfully!" -ForegroundColor Green } catch { Write-Error $_ } } Else { Write-Warning "The specified path is not found,please try again(Path is case-sensitive)." }Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue $web = Get-SPWebApplication -Identity $Webapplication $zone = $web.AlternateUrls[0].UrlZone $iissetting = $web.IisSettings[$zone] $webPath = $iissetting.Path.ToString() + "\web.config" $webcontent = [XML](Get-Content -Path $webPath) $node = $Webcontent.SelectSingleNode($path) If($node) { try { $node.Removeall() $date = Get-Date -Format o $datetime = $date.Substring(0,$date.IndexOf(".")) $backtime = ($datetime -replace "-","") -replace ":","" $destination = $iissetting.Path.ToString() + "\web"+$backtime+"backup.config" Copy-Item -Path $webpath -Destination $destination write-host "Backup web.config to $destination" -ForegroundColor Green $webcontent.save($webpath) write-host "Remove node '$path' from web.config of $webapplication successfully!" -ForegroundColor Green } catch { Write-Error $_ } } Else { Write-Warning "The specified path is not found,please try again(Path is case-sensitive)." }
Prerequisite
Windows Server 2008R2 or higher version
Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.