Create an XML-based representation of one or more PowerShell objects.

      ConvertTo-XML [-InputObject] psobject
         [-As {String | Stream | Document} ]
            [-Depth int] [-NoTypeInformation] [CommonParameters]

   -As string
       Format as: a String (returns a single string),a Stream (returns an array  
       of strings) or a Document (returns an XmlDocument)

   -CssUri Uri
       The Uniform Resource Identifier (URI) of the cascading style sheet
       The CSS URI is included as a style sheet link in the output.

   -inputObject psobject
       The object(s) to represent as XML.
       A variable that contains the object(s) or a command/expression that gets the object(s).

       When the -InputObject parameter is used to submit a collection of items,
       ConvertTo-XML receives one object that represents the collection.
       Because one object cannot be converted, ConvertTo-XML returns the entire collection unchanged.

       To convert multiple items, pipe them to ConvertTo-XML.

       This parameter is an implementation detail: its purpose is to enable input via the pipeline, and its
       direct use with arrays (collections) does not (yet) provide any useful functionality.

       Omit the Type attribute from the object nodes.

   -Depth int
       The number of levels of contained objects to include.
       The default value is 1.

       For example, if an object’s properties also contain objects,
       specify a depth of 2 to include them.

       The default value can be overridden in Types.ps1xml

       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

If you submit multiple objects, such as all of the services on a computer,ConvertTo-Xml will display the properties of the collection / array.
To display the individual objects, use the pipeline operator to pipe the objects to ConvertTo-Xml one at a time.

Export-Clixml is the same as ConvertTo-XML, except that it saves to a file.


Display the date as XML on the console :

PS C:\> Get-Date | ConvertTo-XML

Convert 3 levels of the system processes to XML:

PS C:\> Get-Process | ConvertTo-XML -depth 3

“If you have an important point to make, don’t try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack” - Winston Churchill

Related PowerShell Cmdlets

Select-XML - Find text in an XML string or document

Copyright © 1999-2024
Some rights reserved