PowerTip: Use PowerShell to Edit XML File

PowerTip: Use PowerShell to Edit XML File

  • Comments 7
  • 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:





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 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)

  • The load command is a method of the object so it needs to be $XMLfile.load(“MyXMLFile.XML”) i.e. a dot not an equals