Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a few applications which store their config files in XML format. For a regular application, using a text based config, I could update a value easily enough by using perl, or sed, or awk, or any one of a million tools. I'm looking for something similar for XML, which will allow me to easily and reliably perform operations like: update a value, add a node, or remove one.

Regular text parsing would seem to be too risky, as I have no real guarantees about the physical file format.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

XML parsing in MS Powershell is easier than any parsing mechanism I've seen in any other language or environment I've personally encountered.

Given some XML file (test.xml):

  <one>I like applesauce</one>
  <two>You sure bet I do!</two>

You can easily access, modify and append nodes, values and attributes of the XML file from inside Powershell.

# load XML file into local variable and cast as XML type.
$doc = [xml](Get-Content ./test.xml)

$doc.root.one                                   #echoes "I like applesauce"
$doc.root.one = "Who doesn't like applesauce?"  #replace inner text of <one> node

# create new node...
$newNode = $doc.CreateElement("three")
$newNode.set_InnerText("And don't you forget it!")

# ...and position it in the hierarchy

# write results to disk

Resulting XML in file testNew.xml:

  <one>Who likes applesauce?</one>
  <two>You sure bet I do!</two>
  <three>And don't you forget it!</three>

Incredibly easy! Enjoy.

Powershell is Microsoft's new shell that ships with Windows Server 2008 and Windows 7 and is a free download for XP/Vista/Server 2003 (perhaps others).

Some useful links:
Generating XML from other sources
Adding elements to XML:
Sample 1, MSDN PowerShell blog
Sample 2, PC-Pro(UK)

share|improve this answer
thanks! . –  Mikeage Jun 17 '09 at 13:09
does that maintain "pretty" xml ? –  djangofan Mar 10 '10 at 17:36
add comment

If I understand the question properly, you are looking for a different configuration file storage format that is akin to XML. One of the best strengths of XML though is the fact it gives you a structured data format. If you violate the DTD or "template" you have a poorly formed XML config file. In the past for some of my own development efforts, scripts, programs and apps, I had used INI formatted files. While INI's are easily parsed, and can support similar add, delete, updates, I favor XML b/c it is:

  1. widely used
  2. Easily Parsed with Perl's XML::LibXML
  3. Offers greater flexibility than INI files
  4. Best solution to persist rigid formats and structs from programs
share|improve this answer
I guess I didn't quite understand the question properly. Now I do; I prefer Perl to PowerShell for portability reasons to other platforms. –  netlinxman Jun 17 '09 at 3:48
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.