Prof. Powershell

XML Marks the Spot Part 1: Conversion

For many IT Pros, XML is the de facto storage standard. XML itself is a complex topic so I'm not going to attempt to teach you XML. What I will do is try to demonstrate some ways you can work with XML in PowerShell. I'm going to be using PowerShell 3 but I believe most everything will also work in PowerShell 2.

First off, I'm assuming you already know about Export-CliXML and Import-CliXML. These cmdlets are used to serialize and deserialize data within PowerShell sessions. The XML files they create aren't really intended for use outside of PowerShell. If you aren't familiar with them, I'll cover them in a future lesson. Instead, let's look at using more traditional XML.

For example, you might need to get information from a server using PowerShell but need to put it into an XML format. In other words, you want to convert data to XML. Lucky for you PowerShell has such a tool called ConvertTo-XML. It works much the same way as the other convert cmdlets like ConverTo-CSV and ConvertTo-HTML. Pipe something to the cmdlet and it will create an XML representation.

PS C:\> get-ciminstance win32_operatingsystem | convertto-xml

xml                                               Objects
---                                               -------
version="1.0"                                     Objects

Oh. That doesn't help me much, although it looks like the cmdlet did exactly what it said it would do: convert to XML. Like the other Convert cmdlets this only converts. Saving the data to a file is a separate step. Let me re-run this command:

PS C:\> $os = get-ciminstance win32_operatingsystem | convertto-xml
PS C:\> $os.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    XmlDocument                              System.Xml.XmlNode

That looks good. I can now work with $os if I wanted. Or I can navigate the document hierarchy as you see in Figure 1.

[Click on image for larger view.] Figure 1.

Although what I typically need to do is simply save the XML document to a file.

PS C:\> $os.Save("c:\work\myos.xml")

You can see the final file in Figure 2.

[Click on image for larger view.] Figure 2.

If you'd like to do convert and save in one command, try something like this:

PS C:\> (Get-CimInstance win32_service | convertto-xml).Save("c:\work\mysvc.xml")

These files can be used in any XML application outside of PowerShell. If your external application or process complains about the XML file, you might need to suppress the type information using the cmdlet's –NoTypeInformation parameter. As always be sure to look at full cmdlet help and examples. In the next lesson we'll look at bringing these, and other XML documents back into PowerShell.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular