sile007
9/5/2017 - 2:07 PM

Read INI File / Regex

Read INI File / Regex

$file = "D:\Powershell\MAUV\config.ini"
Function Parse-IniFile ($file)
{
	$file
	$ini = @{ }
	$section = "_NO_SECTION"
	$ini[$section] = @{ }
	switch -regex (Get-Content $file)
	{
		'^\[(.+)\]$'
		{
			$section = $matches[1].Trim()
			if (-not $ini.ContainsKey($section)) { $ini[$section] = @{ } }
		}
		'^\s*([^#\[;].+?)\s*=\s*(.*)'
		{
			$name, $value = $matches[1 .. 2]
			$ini[$section][$name] = $value.Trim()
		}
		'^\s*([^;\[#][^=]+?)$'
		{
			$name = $matches[1].Trim()
			$value = ''
			$ini[$section][$name] = $value.Trim()
		}
	}
	$ini
}