Remove a set of XML elements with Powershell

Question:

I have something similar to the following in an XML document:

I am able to use Powershell to pull out certain elements within the document (such as grabbing the name or value element based on an index number). However, I don’t follow how those indices work (or whatever the indices may be called). For example, look for name[1] gives me ctrl_btn, but I would have assumed that would be name[0]. Could someone explain that?

My real problem, however, is creating a script that removes certain collections of elements within the document. For example, if I want to remove the following:

I have searched around, but most examples only remove a certain node from the XML and not a whole group as the above.

The name and value are always unique within the XML document (which, by the way, contains over 5000 different name/value combos in the <variable></variable> element.

The list of elements I want to remove are contained within a text file that lists only the name value (e.g., the text document would list ctrl_btn and then ctrl_snpt_calctrl).

To give you better idea, here’s the whole process that the script does:

  1. Get the content of the XML document
  2. Get the value between the <name></name> elements for all items in the document
  3. A search is run against a list of files to verify that the name is used within the file.
  4. If the name is not used, it is written a text file (e.g., not_found.txt).
  5. Remove the <variable></variable> tags and everything between them if the ` is equal to the name found in the not_found.txt.

I’m not sure if the process above helps with an explanation, but I thought I better include it.

So how do you remove these?

Answer:

Assuming you have a string array with each element being the name of a variable element you want to remove, you could do the following:

I am able to use Powershell to pull out certain elements within the document (such as grabbing the name or value element based on an index number). However, I don’t follow how those indices work (or whatever the indices may be called). For example, look for name[1] gives me ctrl_btn, but I would have assumed that would be name[0]. Could someone explain that?

Xml order is not guaranteed, so don’t rely on this. On my machine, for example, I get the results you expect:

Source:

Remove a set of XML elements with Powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply