There are often times where you need to store data within a file to make your script work, examples include:
Complex configuration information
Complex data storage
There are a number of ways you can address this when writing a normal .Net application, but the majority of these can easily be used within Powershell. These options include simple text files, SQL databases and what I want to talk about today, XML files!
Using XML files is very simple in PowerShell, most of the hard work will be performed by the Powershell engine and the .Net libraries it is built upon.
Example XML File
Reading data within an XML file
To read an XML file into a variable, simply
Now if we wanted to access all of the XML elements under the "Folders" element, all we would have to do is:
But if there are no "folder" elements below "folders", you will have a nice error message when you go to run that foreach loop! You can resolve this by testing if there are elements under the "folders" element by:
if the above returns false, then there are no elements sitting under "folders". Note that this command is CASE SENSITIVE!!! folders and Folders are not the same. BEWARE!
This is enough for today, and should allow you to understand some of the scripts I will be posting up soon.