Configuration Files in PowerShell

Developing a mini build and configuration system with PSake recently and was getting annoyed with the number of hard-coded, machine- and environment-specific strings there were so I did some research on alternatives to storing configuration files external to the script. Once again, the PowerShell team have thought about this and provide a solution in the shape of a psd (PowerShell Data) file - .psd1. Data can be structured hierarchically either as key value pairs or hashes. The easiest way to create a file is to generate it from a scratch script using a here string:


@"
@{
    Agent = @{

        Version = '1.0.0'
        Debug = 'true'
    }

    LocalFolders = @{
    
        PowerShellScripts = 'C:\Scripts\PS'
    }
}

"@ | Out-File -FilePath 'Agent.psd1' -Force


Then read it back in in the worker script and reference sub-objects using normal dot notation.


$Config = Import-LocalizedData -FileName Agent.psd1

$Config.Agent.Version