Feb 10

Storing values in XML files from Powershell

As I have had to start learning Powershell over the last few months, I decided I would start posting some of the helpful things I had learned in hopes that others will not have to search as hard for the same information in the future.

There are many reasons why you may want to use XML with Powershell. You may want to store information in Powershell or you may want to read information from PowerShell. Either reason is a good one. I did both. One reason I used XML files was because my script would kick off a half-dozen jobs as well as other Powershell scripts. Using an XML file made it easy to pass information between the jobs and other scripts, which allowed for it to be more dynamic.

So, how is it done? there are several ways depending on how you write your XML.

Reading XML

The first format of XML I will show you uses ‘add key’ and the value within the same brackets. There is nothing wrong with this and it actually can be quite helpful to write the XML like this, especially if you do a ‘foreach’ statement in your PowerShell code.

Now, to read this in PowerShell you must first tell your session you want to read XML. This is done with the code below:

PowerShell requires the [xml] so that it knows what language it is going to read. You must then assign a variable name for the xml file. the ‘Get-Content’ command is then used and you must specify the location of the file (of course!). So, that being said I may write the code like this:

ok, great. So now powershell can read our XML file. now how do we use the values found in the XML file in our PowerShell session?

Retrieving Values from the XML file

Using the example above that uses the add key commands, we would search for the target values like this:

so what this does is it takes the variable for reading the XML file ($MyConfigFile) and by specifying the section of the XML to look in (.configuration.General.add (look back in the XML example above and you’ll see that the sections are in the same order in the Powershell script)) you can then use an ‘if’ or a ‘switch’ to return the value and assign it to a variable.

Is there an easier way?

Like almost everything in this world, there is always more than one way to skin a cat. Below is another example that doesn’t require the use of ‘if’ statements or ‘switch’es.

You will note that this part of my XML is different in that the name of the object is in one bracket and the value is contained between the opening and closing of the object (like what you would see in HTML)

We can then reference each object by starting with our XML variable and then working our way through our XML hierarchy to the desired object:

Saving values to XML

Saving values is just as easy as reading values from the XML files. All you have to do is change what is on each side of the ‘=’. To save back to the file you could do this:

You must then use the ‘.Save(XMLfile)’ to make sure the new values are saved and not discarded.

once the values are saved, you can use them across any PS Sessions, Jobs, Scripts, or any other program that you may choose to share data with.

1 comment

  1. samar siddiquie

    Great.Hat’s off for your patience writing and solve from hell

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

5 × one =