When working with PowerShell scripts in production environments, using configuration files (INI, CONF, or similar formats) provides significant advantages:
- Separation of settings from code logic
- Easier maintenance without script modification
- Simpler deployment across different environments
PowerShell has built-in capabilities for working with structured configuration files:
# Sample config.ini
[Links]
Link1 = http://www.google.com
Link2 = http://www.apple.com
Link3 = http://www.microsoft.com
For simple key-value pairs:
$config = Get-Content -Path 'config.ini' -Raw | ConvertFrom-StringData
Start-Process $config.Link1
For more complex INI files with sections:
function Get-IniContent {
param($FilePath)
$ini = @{}
switch -regex -file $FilePath {
"^$$(.+)$$" {
$section = $matches[1]
$ini[$section] = @{}
}
"^\s*([^#].+?)\s*=\s*(.*)" {
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
$config = Get-IniContent "config.ini"
Start-Process $config.Links.Link1
For enterprise-grade solutions:
Install-Module -Name PSFramework
Import-Module PSFramework
# Create configuration
Set-PSFConfig -Module MyModule -Name Links.Link1 -Value "http://www.google.com"
Export-PSFConfig -OutPath config.json
# Import later
Import-PSFConfig -Path config.json
Start-Process (Get-PSFConfigValue -FullName MyModule.Links.Link1)
For configuration validation:
[ValidatePattern('^https?://.+')]
[string]$Link1 = "http://default.com"
For environment-specific configurations:
$env = $env:COMPUTERNAME.Substring(0,3)
$configFile = "config.$env.ini"
- Always validate configuration values
- Consider using signed configuration files
- Store sensitive data in encrypted formats
Configuration files (INI, CONF, etc.) are commonly used to store settings and parameters for scripts. PowerShell provides several ways to read and parse these files efficiently.
For simple key-value pairs, you can use PowerShell's built-in cmdlets:
# Sample config.ini file content:
# [Links]
# link1=http://www.google.com
# link2=http://www.apple.com
$configFile = "C:\path\to\config.ini"
$content = Get-Content $configFile | Where-Object { $_ -notmatch '^;|^\s*$' }
$config = @{}
$content | ForEach-Object {
if ($_ -match '^$$(.+)$$') {
$section = $matches[1]
}
elseif ($_ -match '^(.+?)\s*=\s*(.*)') {
$config["$section.$($matches[1])"] = $matches[2]
}
}
# Access values:
Start-Process iexplore.exe $config["Links.link1"]
For more complex scenarios, consider using the PSFramework module:
Install-Module -Name PSFramework -Force
Import-Module PSFramework
# Create configuration schema
$configSchema = @{
Links = @{
link1 = 'http://www.google.com'
link2 = 'http://www.apple.com'
}
}
# Register configuration
Register-PSFConfig -FullName 'MyScript.Settings' -Schema $configSchema
# Import from file
Import-PSFConfig -Path 'C:\path\to\config.psd1'
# Access values
Start-Process iexplore.exe (Get-PSFConfigValue -FullName 'MyScript.Settings.Links.link1')
PowerShell can work with various configuration file formats:
INI Files
# Using third-party module
Install-Module -Name PSParseIni -Force
$iniContent = Get-IniContent -FilePath 'C:\path\to\config.ini'
Start-Process $iniContent['Links']['link1']
JSON Files
$jsonConfig = Get-Content 'C:\path\to\config.json' | ConvertFrom-Json
Start-Process $jsonConfig.Links.link1
XML Files
[xml]$xmlConfig = Get-Content 'C:\path\to\config.xml'
Start-Process $xmlConfig.config.links.link1
- Always validate configuration values before use
- Implement default values for missing configurations
- Consider using environment-specific configuration files
- Secure sensitive configuration data using PowerShell's SecretManagement
try {
$config = Get-Content 'config.json' -ErrorAction Stop | ConvertFrom-Json
if (-not $config.Links.link1) {
throw "Required configuration 'link1' is missing"
}
Start-Process $config.Links.link1
}
catch {
Write-Error "Configuration error: $_"
# Fallback to default value
Start-Process 'http://www.default.com'
}