Skip to content

Instantly share code, notes, and snippets.

@msm-fc
Created November 2, 2018 17:36
Show Gist options
  • Save msm-fc/92bb3c2e91a858e8f7842b74a7af3e3d to your computer and use it in GitHub Desktop.
Save msm-fc/92bb3c2e91a858e8f7842b74a7af3e3d to your computer and use it in GitHub Desktop.
Remove all XML child nodes of the same name from a single parent node with PowerShell
#Remove all XML child nodes of the same name from a single parent node
#this works in PowerShell version 5.1 (older versions are not tested)
[xml]$xml = @"
<system.webServer>
<httpProtocol>
<customHeaders>
<!--Override the IE broweser's Compatibility View Settings for intranet sites.-->
<add name="X-UA-Compatible" value="IE=Edge" />
<add name="X-Frame-Options" value="SAMEORIGIN" />
</customHeaders>
</httpProtocol>
<security>
<requestFiltering>
<!--This is needed to support uploading large byte arrays.-->
<requestLimits maxAllowedContentLength="209715200" />
<!--Disabling OPTIONS per security assessment recommendation-->
<verbs allowUnlisted="true">
<add verb="OPTIONS" allowed="false" />
</verbs>
</requestFiltering>
</security>
</system.webServer>
"@
$xml.SelectNodes("system.webServer").ChildNodes | Where-Object {$_.Name -match "httpProtocol"} | %{$_.ParentNode.RemoveChild($_)}
$xml.Save("d:\temp\webconfigexample.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment