PowerShell Genie

ConvertTo-Xml

Use ConvertTo-Xml to convert objects into an XML representation of their properties.

Command 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — ConvertTo-Xml

Syntax

1PS> ConvertTo-Xml [-InputObject] <PSObject> [-Depth <Int32>] [-NoTypeInformation] [-As <String>] [<CommonParameters>]
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>]
  1. Stage 1ConvertTo-Xml-InputObject -Depth -NoTypeInformation -As

    Try it

    Windows PowerShell

    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.

    Interactive simulation · fictional data

    Examples

    Select first 2

    1PS> Get-Service | Select-Object -First 2 Name, Status | ConvertTo-Xml -As String -NoTypeInformation
    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

    1PS> Get-Process | Select-Object -First 1 Name, Id | ConvertTo-Xml -As String -Depth 2
    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)

    1PS> Get-Service | Select-Object -First 1 Name, Status | ConvertTo-Xml -As String -NoTypeInformation
    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

    ConvertTo-Xml on Microsoft Learn

    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.

    ConvertTo-Xml reference on Microsoft Learn

    Recap

    ConvertTo-Xml Use ConvertTo-Xml to convert objects into an XML representation of their properties.

    Next: ConvertTo-Json
    Genie guideConvertTo-Xml

    Use ConvertTo-Xml to convert objects into an XML representation of their properties. The console below is pre-loaded — run it, then change a value and run it again.