powershell ini file parsing add new item

Boyd Mills 21 Reputation points
2023-05-04T15:41:54.2333333+00:00

I am trying to do something I thought rather simple:

    Import-Module PsIni
    $ini=Get-IniContent C:\windows\vcitest.ini
    $ini.Add("Setup2","bUSEOWSUDP[Y/N]" )

    $ini["Setup2"]["bUSEOWSUDP[Y/N]"]="Y"

    $ini["Setup"]["USEOWSUDP[Y/N]"]="Y"
    $ini["Setup"]["UDP"]="Y"

This works perfectly as long as the section and entry

["section"]["entry"]

already exists but fails miserably when trying to create a new section or entry.

Please help.

Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-05-04T20:20:29.8066667+00:00

    Like this:

    Import-Module PsIni
    $ini = Get-IniContent C:\junk\ini.ini
    
    $ini | Set-IniContent -NameValuePairs @{"bUSEOWSUDP[Y/N]"="Y"} -Sections 'Setup2'
    $ini | Set-IniContent -NameValuePairs @{"USEOWSUDP[Y/N]"="Y"; "UDP" = "Y"} -Sections 'Setup'
    
    

    Or, if you prefer not to use the cmdlet to do that:

    Import-Module PsIni
    $ini = Get-IniContent C:\junk\ini.ini
    
    $ini.Add("Setup2",@{"bUSEOWSUDP[Y/N]"="Y"})
    $ini.Add("setup",@{"USEOWSUDP[Y/N]"="Y"; "UDP" = "Y"})
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.