Last active
March 28, 2020 15:11
-
-
Save celsojr/4ec3687f1100c5ce2c6d926ff8ef56d4 to your computer and use it in GitHub Desktop.
Exclude a node containing a specific text in it with PowerShell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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