How to read parameters from an ini file

An example on how to read parameters from an ini file.

<#
.SYNOPSIS
	Parameters - An example on how to read parameters from an ini file.
.NOTES
    File Name  : Parameters.ps1 
    Author     : Fabrice ZERROUKI - fabricezerrouki@hotmail.com
.EXAMPLE
	PS D:\>Parameters.ps1
#>
Write-Host "Reading parameters from 'parameters.ini' file..." -ForegroundColor DarkGreen;
Get-Content "parameters.ini" | ForEach-Object -Begin {$settings=@{}} -Process {$store = [regex]::split($_,'='); if(($store[0].CompareTo("") -ne 0) -and ($store[0].StartsWith("[") -ne $True) -and ($store[0].StartsWith("#") -ne $True)) {$settings.Add($store[0], $store[1])}}

Write-Host "Integrating parameters from 'parameters.ini' file in variables... `n" -ForegroundColor DarkGreen;
$Param1 = $settings.Get_Item("Param1")
$Param2 = $settings.Get_Item("Param2")
$Param3 = $settings.Get_Item("Param3")

# Displaying the parameters
Write-Host "Param1 value is: $Param1" -ForegroundColor DarkYellow;
Write-Host "Param2 value is: $Param2" -ForegroundColor DarkYellow;
Write-Host "Param3 value is: $Param3`n" -ForegroundColor DarkYellow;

Content of the ‘parameters.ini’ file

#################################################################################
#										#
#	File	: parameters.ini						#
#	Purpose	: provide settings for the parameters.ps1 script		#
#										#
#										#
#	Author	: Fabrice ZERROUKI - fabricezerrouki@hotmail.com		#
#										#
#################################################################################

[Param1]
#Expected value: Param1=An Example Of The Expected Value For Param1
Param1=xxxxxxxxxx

[Param2]
#Expected value: Param2=An Example Of The Expected Value For Param2
Param2=yyyyyyyyyy

[Param3]
#Expected value: Param3=An Example Of The Expected Value For Param3
Param3=zzzzzzzzzz

Leave a Reply

Scroll to Top