Use this when PowerShell reads or changes XML with [xml], Select-Xml or Clixml.
XML · PowerShell Automation
PowerShell handles XML with [xml] type casting, Select-Xml with XPath, and Export-Clixml and Import-Clixml for saving objects, including encrypted credentials for the same user.
1. PowerShell [xml] type accelerates XML parsing.
2. Select-Xml for XPath with namespaces.
3. Avoid untrusted DTDs.
4. Modify and Save.
5. Validate against XSD if needed.
XML file → [xml] → XPath/edit → save
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
Load a representative document, inspect DocumentElement, namespace URI and selected node counts, then test XPath queries with the correct namespace mapping. After mutation, save and reparse the file so validation covers the serialized XML, not just the in-memory DOM. Verify the consuming application accepts the resulting document.
Step 2 Common mistakes Narrow the fault
- Treating XML as text.
Step 3 Where it fails Narrow the fault
Well-formed XML is not necessarily schema-valid or semantically accepted by the consumer. Node selection, namespace binding, mutation and serialization are separate things to check. Avoid regex/text replacement for structured XML when DOM/XPath or schema-aware tooling can preserve the document contract.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- [xml] casts text to an XML document.
- Select-Xml queries with XPath.
- Export-Clixml saves objects with types.
[xml]$x = Get-Content f.xml— parseSelect-Xml -Path f.xml -XPath '//node'— XPath$x.Save('out.xml')— write
ReferenceWhy it matters1 section · depth on demand
In depth
PowerShell reads XML into an [xml] object, giving dot-notation access to elements and attributes, plus XPath queries through Select-Xml and SelectNodes.
[xml]$x = Get-Content .\export.xml -Raw
$x.inventory.device | Select-Object name, site
Select-Xml -Xml $x -XPath '//device[@role="core"]' | ForEach-Object { $_.Node.name }
$new = $x.CreateElement('device'); $new.SetAttribute('name','sw-oslo-09'); [void]$x.inventory.AppendChild($new)
$x.Save("$PWD\export.xml")
With namespaces, create an XmlNamespaceManager and pass it to SelectNodes. Export-Clixml/Import-Clixml are different: they serialise PowerShell objects (including types) and are meant for PowerShell-to-PowerShell exchange, not general XML.
ReferenceImportant terms & boundaries1 section · depth on demand
PowerShell mechanics
PowerShell can load XML into XmlDocument objects through [xml] casts and can query documents with Select-Xml/XPath. Element namespaces are part of node identity, so an XPath that ignores the document's namespace can return no nodes even when visually matching elements exist. Encoding, whitespace, attributes/elements and document mutation/save behavior are distinct from the formatted object view.
ReferenceConfiguration & state1 section · depth on demand
Authoring and lifecycle
Treat XML as a schema- and namespace-aware document contract. Preserve declared encoding and namespaces, validate against XSD when the integration provides one, and use an explicit namespace manager/map for XPath. When modifying documents, write to a controlled output and compare/validate before replacing authoritative configuration.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Configuration & expected output
Concrete examples are shown only when the stored reference contains an actual configuration or output shape for this Topic.
Configuration example
Assumptions: Review target identity, permissions and environment before mutation.
[xml]$doc = Get-Content -LiteralPath $Path -Raw
Select-Xml -Xml $doc -XPath "/root/item"Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Microsoft Learn documentation reviewed for bounded PowerShell reference claims. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-xml?view=powershell-7.5 · confidence 94%Quick lookup tables
XML · PowerShell operational reference
Topic-specific PowerShell runtime identity, persistence, verification, output and failure-boundary lookup.
| Boundary | XML reference |
|---|---|
| Runtime identity | PowerShell edition/version + command/module source |
| Persistent intent | Script/module/config prerequisites under source control |
| Verification | Read-only discovery → execution evidence → independent target read-back |
| Output contract | Structured objects/streams before formatting |
| Failure boundary | Separate engine/binding from transport/API/native and target-state failures |
Deep reference
Healthy vs suspicious evidence
Output interpretation
PowerShell formatting can make attributes and child elements look similar even though XPath/schema semantics differ. A zero-node XPath result is often a namespace/query issue rather than evidence the element is absent. The in-memory DOM can be valid while saved encoding or external schema requirements still fail.