0

I have this script below which filters out a list of xmls based on the attribute value:

gci $path | Select-Xml -XPATH "//Order[contains(@Value, '0')]"

I am trying to filter out those XMLs with a "value of zero". The problem is that when I run the script against my source, it returns everything that has a "0" in the value. How can I change the filter to only filter on "0", ie. no value greater than zero? I guess there is a filter to replace "contains" to something like "match", but I've tried this. Can't find any alternatives on the Internet.

1 Answer 1

1

Use a Where-Object filter with a -not condition to exclude all files where your XPath expression produces a match:

Get-ChildItem $path | Where-Object {
  -not (Select-Xml -Path $_.FullName -XPath "//Order[contains(@value, '0')]")
}

Note that this might produce false negatives, though (e.g. if the attribute value contains a value like 10, 201, …). If you only want to exclude files where an attribute value is exactly 0 you can do so with a numeric comparison:

Get-ChildItem $path | Where-Object {
  -not (Select-Xml -Path $_.FullName -XPath "//Order[@value = 0]"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Ansgar. I have found an alternative way of doing this now: $All1 = gci $path | Select-Xml -XPATH //Order | Select-Object -ExpandProperty node $Filtered = $All | Where {$_.Lines -eq "0"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.