Created
September 24, 2024 13:48
-
-
Save krokofant/d3a1f5f01f09a8bb2d835ffd8e8708a3 to your computer and use it in GitHub Desktop.
Get-IniContent adopted and updated from https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-any-ini-file/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-IniContent ($filePath) { | |
$ini = @{} | |
switch -regex -file $FilePath { | |
"^\[(.+)\]" { # Section | |
$section = $matches[1] | |
$ini[$section] = @{} | |
$CommentCount = 0 | |
continue | |
} | |
"^(;.*)$" { # Comment | |
$value = $matches[1] | |
if ($null -eq $section) { continue } # Ignore global comment | |
$CommentCount = $CommentCount + 1 | |
$name = "Comment" + $CommentCount | |
$ini[$section][$name] = $value | |
continue | |
} | |
"(.+?)\s*=(.*)" { # Key | |
$name, $value = $matches[1..2] | |
$ini[$section][$name] = $value | |
continue | |
} | |
} | |
return $ini | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment