-
-
Save ildar-shaimordanov/4064608cc8f786a4cf5ab04541eb9695 to your computer and use it in GitHub Desktop.
Powershell to/from INI Files
This file contains 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 | |
} | |
"^(;.*)$" # 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 | |
} |
This file contains 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 Out-IniFile($InputObject, $FilePath) | |
{ | |
$outFile = New-Item -ItemType file -Path $Filepath | |
foreach ($i in $InputObject.keys) | |
{ | |
if (!($($InputObject[$i].GetType().Name) -eq "Hashtable")) | |
{ | |
#No Sections | |
Add-Content -Path $outFile -Value "$i=$($InputObject[$i])" | |
} else { | |
#Sections | |
Add-Content -Path $outFile -Value "[$i]" | |
Foreach ($j in ($InputObject[$i].keys | Sort-Object)) | |
{ | |
if ($j -match "^Comment[\d]+") { | |
Add-Content -Path $outFile -Value "$($InputObject[$i][$j])" | |
} else { | |
Add-Content -Path $outFile -Value "$j=$($InputObject[$i][$j])" | |
} | |
} | |
Add-Content -Path $outFile -Value "" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment