Skip to content

Instantly share code, notes, and snippets.

@quonic
Created January 21, 2017 16:28
Show Gist options
  • Save quonic/64816cc8ee57c91e2332ee9c6386699e to your computer and use it in GitHub Desktop.
Save quonic/64816cc8ee57c91e2332ee9c6386699e to your computer and use it in GitHub Desktop.
$myIniSettings = Get-IniContent mySettings.ini
$myIniSettings | ConvertTo-Json
<# Returns
{
"second": {
"dotheneedful": "1234",
"donothing": "\"doing it\""
},
"first": {
"setting2": "lol what",
"setting1": "true"
}
}
#>
$myIniSettings.first.setting1
# Returns: true
# https://blogs.technet.microsoft.com/heyscriptingguy/2011/08/20/use-powershell-to-work-with-any-ini-file/
function Get-IniContent ($filePath)
{
$ini = @{}
switch -regex -file $FilePath
{
“^\[(.+)\]” # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
“^(;.*)$” # Comment
{
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = “Comment” + $CommentCount
$ini[$section][$name] = $value
}
“(.+?)\s*=(.*)” # Key
{
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
[first]
setting1=true
setting2=lol what
[second]
dotheneedful=1234
donothing="doing it"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment