ConvertTo-Xml
Use ConvertTo-Xml to convert objects into an XML representation of their properties.
Documentation: Microsoft Learn — ConvertTo-Xml
Syntax
See the command anatomy
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
ConvertTo-Xml [-InputObject] <PSObject> [-Depth <Int32>] [-NoTypeInformation] [-As <String>] [<CommonParameters>]- Stage 1ConvertTo-Xml
-InputObject -Depth -NoTypeInformation -As
Try it
PowerShell example · browser support is limited.
Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.
Follow the objects
Run the command to inspect each stage.
Examples
Select first 2
Show reference output
Sample output · fictional data, shaped like the real thing
<?xml version="1.0"?> <Objects> <Object><Property Name="Name">BITS</Property><Property Name="Status">Running</Property></Object> </Objects>
Select first 1
Show reference output
Sample output · fictional data, shaped like the real thing
<?xml version="1.0"?> <Objects> <Object><Property Name="Name">chrome</Property><Property Name="Id">9184</Property></Object> </Objects>
Select first 1 (2)
Show reference output
Sample output · fictional data, shaped like the real thing
<?xml version="1.0"?> <Objects> <Object><Property Name="Name">BITS</Property><Property Name="Status">Running</Property></Object> </Objects>
Parameters
Parameter definitions:
-InputObject-Depth-NoTypeInformation-As
Practical use and troubleshooting
Practical scenario: Use ConvertTo-Xml when you need to convert objects into an XML representation of their properties. Review syntax and sample output, then try an example in the browser lab. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.
Common error and fix: Unexpected XML output can come from the selected output mode. Choose the required representation explicitly and validate it with the consuming application before using it as an interchange file. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.
See also: ConvertTo-Json. ConvertTo-Xml represents objects as XML; ConvertTo-Json serializes them as JSON text. Choose the format required by the consumer and check the resulting structure rather than relying on display appearance. Read the linked command before substituting it; similar names do not imply identical inputs or output.
Recap
ConvertTo-Xml Use ConvertTo-Xml to convert objects into an XML representation of their properties.
Next: ConvertTo-Json