PowerTip: Use PowerShell to Edit XML File

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:





Comments (12)

  1. jrv says:

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

  2. @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

  3. jrv says:

    Notice that all comments are stripped of all white space.

  4. Randy Pitkin says:

    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.

  5. jrv says:

    Notice that all comments are stripped of all white space.

  6. jrv says:

    If you do not use full path the load and save will be unreliable. This line is wrong: $XMLfile=load(“MyXMLFile.XML”)

  7. jrv says:

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

  8. jrv says:

    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

  9. JV says:

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

  10. John says:

    jrv, you’re really hung on these comments =P

  11. Periwinkle says:

    Completely useless as a real world example. Scripting Guy must be tired of helping newbies, and resorting to giving god-like terse answers. Bad form!

  12. David says:

    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

Skip to main content