0

Thanks to the help of @Mathias R. Jessen i was able to get this working but what I am trying to do is add a variable on the top of the script to set the model name but i am not having any luck. Any ideas?

$modelselect = "HP EliteBook x360 830 G7"

$file = "C:\Temp\test1.xml"
$xmlfile = [XML](Get-Content $file)
$item = Select-Xml $xmlFile -XPath '//Model[./ModelName = "$modelselect"]'

foreach($node in $item.Node)
{
$node.ParentNode.RemoveChild($node)
}

$xmlFile.Save($file)
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Ant Pro
  • 23
  • 1
  • 7
  • The outer single quotes on the expression `'//Model[./ModelName = "$modelselect"]'` are not allowing the variable expansion. `"//Model[./ModelName = '$modelselect']"` should work. – Santiago Squarzon Jan 14 '22 at 18:30
  • 1
    Did you see the [last update to the previous answer](https://stackoverflow.com/a/70714039/712649)? Notice I swapped `'` and `"` in the string literal to allow expansion of `$ModelName` – Mathias R. Jessen Jan 14 '22 at 18:32
  • @Mathias R. Jessen Sorry, I didn't. I will go check. Thank you, appreciate all your help! First time working with XML. – Ant Pro Jan 14 '22 at 18:37
  • In short: Only `"..."` strings (double-quoted, called _expandable strings_) perform string interpolation (expansion of variable values) in PowerShell, not `'...'` strings (single-quoted, called _verbatim strings_): see [this answer](https://stackoverflow.com/a/40445998/45375) for an overview of PowerShell's _expandable strings_ and [this answer](https://stackoverflow.com/a/55614306/45375) for an overview of PowerShell _string literals in general_. – mklement0 Jan 14 '22 at 19:00

1 Answers1

1

The below worked, thank you, everyone!

"//Model[./ModelName = '$modelselect']"
Ant Pro
  • 23
  • 1
  • 7