HOWTO · PowerShell
How to Parse XML Files Using PowerShell
Use Select-Xml, XmlDocument, or XmlTextReader in PowerShell to extract XML elements and attributes.
On this page
To extract values from XML in PowerShell, use Select-Xml with an XPath expression for a quick query, System.Xml.XmlDocument when you need to navigate a loaded document, or System.Xml.XmlTextReader to read it forward as a stream. The examples below read XML only. They use a small computer inventory and show how element text differs from attribute values.
Parse XML elements and attributes with Select-Xml
Select-Xml evaluates an XPath expression and returns match objects. Each match exposes the selected XML node through its Node property. Use InnerText to read an element’s text; InnerXml returns markup contained inside the element, which is useful when that content itself is needed as XML.
Run this example:
$path = Join-Path ([System.IO.Path]::GetTempPath()) "computers-$([guid]::NewGuid()).xml"
@'
<Computers>
<Computer name="WINPC-01" ip="127.0.0.1"><Name>WINPC-01</Name></Computer>
<Computer name="WINPC-02" ip="192.168.1.105"><Name>WINPC-02</Name></Computer>
</Computers>
'@ | Set-Content -Path $path -Encoding utf8
Write-Output 'Element names:'
Select-Xml -Path $path -XPath '/Computers/Computer/Name' |
ForEach-Object { $_.Node.InnerText }
Write-Output 'Attribute names:'
Select-Xml -Path $path -XPath '/Computers/Computer' |
ForEach-Object { $_.Node.GetAttribute('name') }
Remove-Item -Path $path
Element names:
WINPC-01
WINPC-02
Attribute names:
WINPC-01
WINPC-02
The XPath /Computers/Computer/Name follows the hierarchy from the root to each Name element. To select attributes, select the Computer elements and read the name attribute from each matched node. Change the XPath to match the structure of your XML; element names and capitalization must match the document. Select-Xml also accepts XML content through its -Content parameter when the data is already in a string.
Load and query XML with System.Xml.XmlDocument
System.Xml.XmlDocument loads the document into a navigable tree. This is convenient when a script needs several related queries or must move between parent and child nodes. The tradeoff is that the document is held in memory, so a streaming reader may fit better for very large files.
This example loads a temporary XML file, selects each computer, and checks for a Name child before reading it. The guard matters when the XML schema permits that element to be absent; SelectSingleNode() returns $null when there is no match.
$path = Join-Path ([System.IO.Path]::GetTempPath()) "computers-$([guid]::NewGuid()).xml"
@'
<Computers>
<Computer><Name>WINPC-01</Name></Computer>
<Computer><Ip>192.168.1.105</Ip></Computer>
</Computers>
'@ | Set-Content -Path $path -Encoding utf8
$document = [System.Xml.XmlDocument]::new()
$document.Load($path)
foreach ($computer in $document.SelectNodes('/Computers/Computer')) {
$nameNode = $computer.SelectSingleNode('Name')
if ($null -ne $nameNode) {
Write-Output $nameNode.InnerText
}
else {
Write-Output 'No Name element'
}
}
Remove-Item -Path $path
Use InnerText for the text value rather than InnerXml, which returns any markup nested inside the element. If the document has a default XML namespace, unprefixed XPath expressions may not match its elements; create an XmlNamespaceManager and use a prefix in the query.
Read XML as a stream with System.Xml.XmlTextReader
System.Xml.XmlTextReader exposes XML nodes in document order through a forward-only, read-only reader. It avoids constructing a complete document tree, which can reduce memory use for large inputs. Because the reader does not support navigating backward, extract each value as the relevant node passes by. Close it in a finally block so the file handle is released even if processing stops early.
The following example reads the name attribute from each Computer start element. It creates its own XML file and disposes of the reader after processing.
$path = Join-Path ([System.IO.Path]::GetTempPath()) "computers-$([guid]::NewGuid()).xml"
@'
<Computers>
<Computer name="WINPC-01" ip="127.0.0.1" />
<Computer name="WINPC-02" ip="192.168.1.105" />
</Computers>
'@ | Set-Content -Path $path -Encoding utf8
$reader = [System.Xml.XmlTextReader]::new($path)
try {
while ($reader.Read()) {
if ($reader.NodeType -eq [System.Xml.XmlNodeType]::Element -and
$reader.Name -eq 'Computer') {
Write-Output $reader.GetAttribute('name')
}
}
}
finally {
$reader.Dispose()
Remove-Item -Path $path
}
For element text instead of an attribute, test for the matching element name and read its text while the reader is positioned on that element. ReadString() reads the text content and advances the reader, so account for that movement in a more complex loop. Dispose of the reader when finished; leaving it open can keep the input file locked.
Choose a PowerShell XML parsing method
Choose Select-Xml when a short XPath query is the clearest way to retrieve a few nodes from a file or string. Choose System.Xml.XmlDocument when you need convenient tree navigation, multiple queries, or access to related nodes. Choose System.Xml.XmlTextReader when you can process nodes in order and want to avoid loading a full tree into memory.
All three approaches can read elements and attributes. The main difference is how they expose the document: Select-Xml returns XPath matches, XmlDocument supports navigation across a loaded tree, and XmlTextReader moves forward through a stream. Select the approach that fits the document size and query pattern, then check for optional nodes or attributes when the input may omit them.