XML for PowerShell Config

I've really taken a liking recently to using XML files for storing configuration values for my PowerShell scripts. Most times, I do it to make it a "one-stop shop" for updating config values.

XML for PowerShell Config

I've really taken a liking recently to using XML files for storing configuration values for my PowerShell scripts. Most times, I do it to make it a "one-stop shop" for updating config values.

I create a lot of scripts for people who may not have the most experience when it comes to reading code. It's definitely easier to say, "Update config.xml and don't worry about changing anything in the PowerShell script." Most people will look at a PowerShell script and go cross-eyed... this prevents that.

I decided since I like it so much, I'm going to show you how it's done. It's a lot easier than I was expecting it to be... check this out:

Create a new XML file called config.xml and add the following information to it:

<?xml version="1.0"?>
<Settings>
    <Name>Joe Garcia, CISSP</Name>
    <Website>https://joeco.de</Website>
</Settings>
config.xml

Next, create a new PowerShell script in the same directory called XMLConfig.ps1 and add the following information to it:

# Import XML Configuration Settings
[xml]$ConfigFile = Get-Content "config.xml"

# Write to output the stored Name and Website
Write-Output $ConfigFile.Settings.Name
Write-Output $ConfigFile.Settings.Website
XMLConfig.ps1

In the above script, we're importing config.xml into the variable $ConfigFile. Afterwards, we're sending the values of the Name and Website sections of the XML file to the PowerShell Console host.

In just 4 easy steps, you've created your own XML Configuration file to use along side your favorite PowerShell scripts going forward!