powershell xml xpath powershell xml xpath powershell powershell

powershell xml xpath


I have a preference for using XPath these days. I've run into issues using PowerShell's xml adapter that are annoying like the name collision on item:

$xml = [xml]@'  <Users>     <User Name="Foo">       <Friends>         <Friend Name="Bar"/>       </Friends>     </User>     <User Name="Foo2" />     <User Name="Foo3">       <Friends>         <Friend Name="Bar"/>       </Friends>     </User>   </Users> '@Select-Xml '//User[contains(Friends/Friend/@Name, "Bar")]' $xml |%{$_.Node.Name}


In this case I would use XPath as well. @TomWij provided the correct xpath.

In case that the xpath expression would be too complicated, I would for sure used the 'classic' approach.

$x.Users.User | ? { $_.Friends.Friend.Name -eq 'Bar' }

(in this case if you don't have script mode on, it doesn't matter that there is no Friends element. $_.Friends will return $null and $null.Friend returns $null as well and so on. So finally $null -eq 'Bar' returns false and the element is removed by Where-Object)


Yes, XPath would be sufficient for that!

Check Powershell to test your XPath to see how it can be done.