Есть способ использовать WindowsAPI.
$src = @"
[DllImport("KERNEL32.DLL")]
public static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, System.Text.StringBuilder lpReturnedString, uint nSize, string lpFileName);
[DllImport("KERNEL32.DLL")]
public static extern uint WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName);
"@
Add-Type -MemberDefinition $src -Namespace WinApi -Name IniFileIO -Language CSharp
Function Read-IniFile ($Path, $Section, $Key) {
$sb = [Text.StringBuilder]::new(1024)
[WinApi.IniFileIO]::GetPrivateProfileString($Section, $Key, $null, $sb, $sb.Capacity, (Convert-Path -LiteralPath $Path)) > $null
$sb.ToString()
}
Function Write-IniFile ($Path, $Section, $Key, $Value) {
[WinApi.IniFileIO]::WritePrivateProfileString($Section, $Key, $Value, (Convert-Path -LiteralPath $Path)) >$null
}
Использование выглядит следующим образом.
Read-IniFile -Path "C:\sample.ini" -Section "Section1" -Key "Key1"
Write-IniFile -Path "C:\sample.ini" -Section "Section2" -Key "Key2" -Value "Hello"
Это случайный код, поэтому лучше использовать код, написанный mklement0 для фактического использования. Это более надежно.