Skip to content

Instantly share code, notes, and snippets.

@jatubio
Last active March 14, 2016 11:04
Show Gist options
  • Save jatubio/5a61ea513cfa7ee93b03 to your computer and use it in GitHub Desktop.
Save jatubio/5a61ea513cfa7ee93b03 to your computer and use it in GitHub Desktop.
PowerShell. Ini Files. Replacing a value in a key inside a section sample 2
# Ini files
# Replacing a value in a key inside a section sample
#
# RegEx101 Test (https://regex101.com/r/uC0cC3/1)
#
# http://stackoverflow.com/questions/29688299/powershell-and-regex-how-to-replace-a-ini-name-value-pair-inside-a-named-sectio/29688435#29688435
# Ini files
# Replacing a value in a key inside a section sample
#
# Works on RegEx101 (https://regex101.com/r/uC0cC3/1)
#
# But not on PowerShell
#
# http://stackoverflow.com/questions/29688299/powershell-and-regex-how-to-replace-a-ini-name-value-pair-inside-a-named-sectio/29688435#29688435
$Path="C:\Temp\prueba.ini"
@"
[Sky]
Size=Big
Color=White
[Home]
Color=Black
"@ | Set-content $Path
$Section="Sky"
$Name="Color"
$Value="Blue"
(Get-Content $Path) -join "`n" -replace "(?m)(?<=\[$Section\])([^\[]*$Name=)(.+?)$","`$1$Value" | Set-Content $Path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment