PowerTip: Use PowerShell to Edit XML File

PowerTip: Use PowerShell to Edit XML File

  • Comments 10
  • Likes

Summary: Use Windows PowerShell to load an XML file for easy editing.

Hey, Scripting Guy! Question Hey Scripting Guy! Is there an easy way to load and edit an XML file in Windows PowerShell?

Hey, Scripting Guy! Answer It’s so easy that you might miss it if you blink. Create an XML object. With that, you can load, save, and edit properties directly:

$XMLfile=NEW-OBJECT XML

$XMLfile=load(“MyXMLFile.XML”)

$XMLFile.property.otherproperty=”Gibberish”

$XMLFile.save(“MyXMLFile.XML”)

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • If you do not use full path the load and save will be unreliable. This line is wrong: $XMLfile=load(“MyXMLFile.XML”)

  • Notice that all comments are stripped of all white space.

  • Notice that all comments are stripped of all white space.

  • Notice also that clicking on "Post" once can post the same message twice. WHITSPACE 5 LINES MORE WHITESPACE

  • One value in xml is easy, Now lets see you update the an Attribute where there are more than one element of the same type by attribute value. My need comes from an IIS Ap.config layer I need to change the authentication provider on 3 out of 4 sites across 100 web servers. Where that authentication provider is not already set to the correct value.

  • @jrv You are correct, specifying the explicit path to the XML file is definitely required. It's a finicky Object... Just like Morris the Cat @randy Pitkin If you read the previous post regarding Hyper-V you should see that multiple elements are in fact updated using this same method, the trick is finding the direct path to access them with XML. Sean

  • The easy way to load current folder file: $xml=[xml](cat myfile.xml)

  • I see that now the comments are completely broken. No comments are visible at all.

  • Ok - compatibility view loses all comments. Turn off CV and comments appear but the formatting is still lost. So far this happens on all versions of Windows 7,8,2008 IE 10,11

  • jrv, you're really hung on these comments =P