Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

XML · PowerShell Automation

Direct answer

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.

When to use this

Use this when PowerShell reads or changes XML with [xml], Select-Xml or Clixml.

First 30 seconds

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.

Operational verification

XML file → [xml] → XPath/edit → save

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
PowerShell 7.5/current
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformPowerShell 7.5/current · Automation implementations are tool and platform specific. Use the adjacent Domain for the technology being automated and this Domain for automation behavior.
Scope / prerequisiteUse this when PowerShell reads or changes XML with [xml], Select-Xml or Clixml.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

3 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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 references

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.

observation · 1
Key points
  • [xml] casts text to an XML document.
  • Select-Xml queries with XPath.
  • Export-Clixml saves objects with types.
Key checks
  • [xml]$x = Get-Content f.xml — parse
  • Select-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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
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
Technical examples

Configuration & expected output

Concrete examples are shown only when the stored reference contains an actual configuration or output shape for this Topic.

Configuration example

PowerShell 7.5/current 7.5/currentMinimal authored PowerShell pattern for XML

Assumptions: Review target identity, permissions and environment before mutation.

[xml]$doc = Get-Content -LiteralPath $Path -Raw
Select-Xml -Xml $doc -XPath "/root/item"
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docPowerShell 7.5/current 7.5/current

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%
Embedded reference data

Quick lookup tables

XML · PowerShell operational reference

Topic-specific PowerShell runtime identity, persistence, verification, output and failure-boundary lookup.

BoundaryXML reference
Runtime identityPowerShell edition/version + command/module source
Persistent intentScript/module/config prerequisites under source control
VerificationRead-only discovery → execution evidence → independent target read-back
Output contractStructured objects/streams before formatting
Failure boundarySeparate engine/binding from transport/API/native and target-state failures
Depth when you need it

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.

Confirmation

Confirm action

Continue?