Graham
Graham

Reputation: 8171

How to tell when xml element is one of multiple or not in powershell

I have two powershell scripts:

One using a single <b> element:

$x = [xml] "<a><b>foo</b></a>"
$x.a.b.length

And one using multiple (two in this case) <b> elements:

$y = [xml] "<a><b>foo</b><b>bar</b></a>"
$y.a.b.length

The first script returns the length of the text in the one <b> element, the second returns the number of <b> elements.

How do I find out if there is one or multiple <b> elements. Clearly the length property wont do the job.

I'm using win 8.1 and powershell 4

Upvotes: 1

Views: 81

Answers (1)

Joey
Joey

Reputation: 354714

Use Count instead:

PS> $x.a.b.Count
1
PS> $y.a.b.Count
2

Upvotes: 3

Related Questions