
close
close
Today we’ll continue looking at working with XML in PowerShell. Now, the title of this article might be a bit misleading, as we aren’t actually going to convert PowerShell to XML. But rather, I want to demonstrate different ways you can take results from a PowerShell expression or command and turn them into XML. This process is known as serialization and is a terrific technique for saving data that you can re-use later in PowerShell.
Because the XML format is hierarchical, it is a perfect vessel for storing all of the rich object information we get in PowerShell. But a word of caution. When you are considering turning the results of a PowerShell command into an XML document, the first question should be “How am I going to use the result?” Do you plan on using the data in a PowerShell session? Do you need to use the file in an external XML-aware application? Does the file need to be easily read by a human? There are different tools and techniques you can employ depending on your requirements.
For the majority of your XML needs, I am expecting Export-Clixml to be your command of choice. This cmdlet is designed to take the output of any PowerShell expression, convert it to XML and save the results to a file. The primary purpose of this command is to create an XML document that you intend to bring back to life in a PowerShell session. The XML document that is created will most likely not be recognized by traditional or external XML applications. Let’s take a look.
Suppose you have this simple command:
advertisment
Get-Service wuauserv -ComputerName chi-dc04,chi-p50,chi-core01
And you want to save the results to an XML file. All you need to do is pipe the command to Export-Clixml and specify the name for your output file, as shown below:
Get-Service wuauserv -ComputerName chi-dc04,chi-p50,chi-core01 | Export-Clixml -Path c:\work\wu.xml
It is very important that your expression write objects to the pipeline. Anything that uses Write-Host won’t write to the pipeline, which means you can’t serialize it. Don’t include any formatting commands, either. Technically, this will run without error.
Get-Service wuauserv -ComputerName chi-dc04,chi-p50,chi-core01 | format-table -AutoSize | Export-Clixml -Path c:\work\wu.xml
But when the time comes to bring the file back to life in PowerShell, you won’t get the results you expect.
Let’s take a quick look at the result:
advertisment
An alternative cmdlet is ConvertTo-XML. Like Export-Clixml, you should pipe objects to this cmdlet and avoid including any formatting directives.
$x.Save("c:\work\wu2.xml")
I recommend always using a full path and file name. The resulting file is a truer XML file.
Get-Service wuauserv -ComputerName chi-dc04,chi-p50,chi-core01 | ConvertTo-Xml -as String
You’ll get the same results as with -Stream, but it will be one long string. Again, you can modify the string and then save the results to a file. Or simply convert and export in one command.
Get-Service wuauserv -ComputerName chi-dc04,chi-p50,chi-core01 | ConvertTo-Xml -as string | Set-Content -path c:\work\wu3.xml
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri