Creating and Using a Configuration File for Your PowerShell Scripts

.NET has this nice and easy ability to load application configuration information from an XML config file associated with the application.  Unfortunately there isn’t a way to put config info in a config file associated with a DLL e.g. Acme.dll.config.  The only way to get config information from the .NET System.Configuration types is to put your config info in the application’s config file.  For PowerShell scripts this would mean adding the config info to PowerShell.exe.config.  That just seems wrong to me.
 
Considering how well PowerShell supports XML it is pretty easy to consume config information from a similarly formatted config file.  Take this config file as an example:
 

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727" safemode="true"/>
    <requiredRuntime version="v2.0.50727" safemode="true"/>
  </startup>
  <appSettings>
    <add key="Editions" value="Standard Edition,Desktop Edition,Desktop Engine,Evaluation Edition,Enterprise Edition,Developer Edition,Personal Edition,Enterprise Edition (64-bit),Developer Edition (64-bit),Enterprise Evaluation Edition,Small Business Server Edition" />
    <add key="Categories" value="Backup and Recovery,Configuration Options,Database Design,Database Administration,Deprecation,Full-Text,General Administration,Generic,T-SQL,SQL Server 2005 Readiness" />
    <add key="DataTypes" value="string,int,double" />
    <add key="Database" value="" />
    <add key="SQLServer" value="" />
    <add key="WindowsAuthentication" value="" />
    <add key="MaxScanDetailRows" value="500" />
    <!– Logging level. 0 – Off, 1 – Error, 2 – Warning, 3 – Info, >=4 – Verbose –>
    <add key="LoggingLevel" value="2" />
  </appSettings>
</configuration>

 
Save the following to a script called LoadConfig.ps1:
 
param($path = $(throw "You must specify a config file"))
$global:appSettings = @{}
$config = [xml](get-content $path)
foreach ($addNode in $config.configuration.appsettings.add) {
 if ($addNode.Value.Contains(‘,’)) {
  # Array case
  $value = $addNode.Value.Split(‘,’)

  for ($i = 0; $i -lt $value.length; $i++) {
    $value[$i] = $value[$i].Trim()
  }
 }
 else {
  # Scalar case
  $value = $addNode.Value
 }
 $global:appSettings[$addNode.Key] = $value
}

 
Now at the top of your scripts you can execute:
 
.\LoadConfig scriptname.config
 
and then access settings like so:
 
$appSettings["MaxScanDetailRows"]
500
 
$appSettings["datatypes"]
string
int
double
 
$appSettings["datatypes"][0]
string 
 

Of course you don’t have to use the same config format that .NET uses.  You could change the XML structure to make it simpler.  Just modify the LoadConfig.ps1 script accordingly.

This entry was posted in PowerShell. Bookmark the permalink.

6 Responses to Creating and Using a Configuration File for Your PowerShell Scripts

  1. Grateful HumanBeing says:

    Nice. +1

  2. Pingback: Technical: Microsoft – SharePoint – List – Programmatically list contents ( Using Web Services) | Learning in the Open

  3. calagan says:

    Hello,
    As I try to implement a configuration file in .xml for my batch scripts, I encounter the
    limitation below when settings are built on other settings like JavaHome, which would uses ProgDir in the example below. I wonder if you have any suggestion to overcome it?
    $appSettings[“ProgDir”]
    D:\Programs
    $appSettings[“JavaHome”]
    $appSettings[“ProgDir”]\Jre\jre7 << I would need it to be resolved as D:\Programs\Jre\jre7" instead

    Thanks in advance for your help,

    calagan

  4. ali says:

    Thank you, appreciated!

    So how can I modify and save the settings into this xml? Is there a solution for that?

Leave a comment