Skip to content

Instantly share code, notes, and snippets.

@celsojr
Last active March 28, 2020 15:11
Show Gist options
  • Select an option

  • Save celsojr/4ec3687f1100c5ce2c6d926ff8ef56d4 to your computer and use it in GitHub Desktop.

Select an option

Save celsojr/4ec3687f1100c5ce2c6d926ff8ef56d4 to your computer and use it in GitHub Desktop.
Exclude a node containing a specific text in it with PowerShell
# sample.xml file content
# <file>
# <categories>
# <category>cat1</category>
# <category>cat2</category>
# <category>removecategory</category>
# <category>keepthisone</category>
# <category>deletethisone</category>
# <category>somethingelse</category>
# </categories>
# <otherstuff></otherstuff>
# </file>
# Powershell
Get-ChildItem sample.xml | % {
[Xml]$xml = Get-Content $_.FullName
$xml | Select-Xml -XPath '//category' | % {
if ($_.InnerXml -eq 'somethingelse') {
$_.ParentNode.RemoveChild($_)
}
}
# $xml.OuterXml | Out-File $_.FullName
echo $xml.OuterXml
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment