How to read the xml file and write its content into a plain text file?

Tags: xml powershell

How to read the below xml file and write its content into a plain text file?


Desired Output:

d:\demo   e:\demo  FileName  Size  Date
d:\sample   e:\sample  Name  FullName  Version Culture


This is a great article on how to manipulate XML from PowerShell.

Well, your comment around your XML is going to cause some parsing issues - but for the sake of answering the question, I am going to assume it isn't actually there in real life. The easiest way to deal with XML in PowerShell is to treat it as a string, and cast it to an XML object:

$xml = [xml](Get-Content c:\temp\config.xml)

Now that you have an xml variable, you can do cool things like:


Which will put the string "d:\demo\" in the pipeline, and in this case no further processing takes place and it outputs it. Grabbing the other elements should be pretty straightforward, though the multiple parameter tags are treated similarly to an array:


Writing output can be as easy as just redirecting the output:

$ >> c:\temp\output.txt

The single greater than sign will overwrite the contents of the file, the double is used for appending.

By : Goyuix

This video can help you solving your question :)
By: admin