Use a XML-file for configuration in PowerShell scripts

Use a XML-file for configuration in PowerShell scripts

Time for some PowerShell!

Almost every PowerShell script need some configuration parameters. With this post I want to show one way, there are others ;-), to parse a config file in XML format and use the values later.

The solution I’m going to show to you contains three files. A PowerShell script, a XML configuration file and a helper PowerShell file that contains a function to parse the config file.

PowerShell XML config files

The config file contains

The helper function to parse the configuration file looks like this

And finally the main script, that will reference the helper function, call it and use the values.

Summary: Store configuration settings in a XML file and use the values in PowerShell script.

2 Comments

rp

Reply

Why build a function to parse the xml data? why not just use [xml]$content = get-content C:dirfile.xml

 

    René Hézser

    Reply

    The function is used for exception handling.

     

Leave a Reply