Programming

Use PowerShell to Count the Number of Elements in XML Files

I needed to know how many pictures were being sent in a collection of XML documents, so I wrote a PowerShell script to count the number of picture elements.

The element I’m looking for is in the $element_xpath variable. The example below is any picture element, anywhere in the document. You could limit this by location or attribute if you wanted to (but I’m always counting elements).

xml-element-counter.ps1

$element_xpath = "//picture"

$total = 0
Get-ChildItem -Recurse -Filter "*.xml" | % {
    [System.Xml.XmlDocument] $document = New-Object System.Xml.XmlDocument
    $document.load($_.FullName)
    $found_elements = $document.selectnodes($element_xpath)
    $total += $found_elements.count
    Write-Host $found_elements.count.ToString("n0") elements in $_.Name
}
Write-Host "---------------------"
Write-Host Total $total.ToString("n0")

This outputs a list of files with the count per file, with a total at the end of the list.

For example, running .\xml-element-counter.ps1 will output:

320 elements in a.xml
111 elements in b.xml
0 elements in c.xml
334 elements in d.xml
1,465 elements in e.xml
508 elements in f.xml
412 elements in g.xml
2,923 elements in h.xml
1,162 elements in i.xml
4,867 elements in j.xml
230 elements in k.xml
250 elements in l.xml
978 elements in m.xml
---------------------
Total 13,560

So there were 13,560 elements found in all those XML files.