Wednesday, August 22, 2012

How to read the value from property file in Powershell script ?


Create a file "scriptname.config" and copy the below xml content and save.

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="folderName" value="powershellFolder" />
    <add key="versionNumber" value="v001" />
  </appSettings >
</configuration>

Open powershell prompt , navigate to the config file folder and execute the below script.

$path = ".\scriptname.config"
$global:properties = @{}
$config = [xml] ( gc $path )
foreach ($addNode in $config.configuration.appSettings.add) {
      $global: properties[$addNode.Key] = $addNode.Value
}

get the values from the key value list "properties" as shown below.

$ properties[" folderName "]
$ properties[" versionNumber "]

2 comments: