0

After doing numerous online searches I have yet to find a way to do this. There are really two parts to my question:

  1. How do you add a new stanza between two existing ones?

    If I have the following stanzas in an existing file:

    [Section 1]
    Host=A
    IP=10.10.10.1
    
    [Section 2]
    Host=B
    IP=10.10.10.2
    

    How can I use Powershell to add:

    [Section 3]
    Host=C
    IP=10.10.10.3
    

    between Section 1 and Section 2 to achieve:

    [Section 1]
    Host=A
    IP=10.10.10.1
    
    [Section 3]
    Host=C
    IP=10.10.10.3
    
    [Section 2]
    Host=B
    IP=10.10.10.2
    
  2. How do I add a new stanza after the last comment line of an existing file?

    For example if I have:

    #
    # Last change 1/1/2021
    #
    
    [Profile sql]
    Profile=HR
    

    How would I add:

    [Profile postgres]
    Profile=Exec
    

    to achieve:

    #
    # Last change 1/1/2021
    #
    
    [Profile postgres]
    Profile=Exec
    
    [Profile sql]
    Profile=HR
    

    in Powershell?

keyama
  • 1
  • 1
    What have you tried, and how has what you've tried failed? Ideally, you should provide a [MCVE] of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. [SO] is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See [Ask] a Good Question. – Jeff Zeitlin Feb 08 '21 at 18:53
  • 1
    (_Having said the above, your file looks like an old Windows INI file. As such, you might find [Dr Scripto's column of 20 Aug 2011](https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-any-ini-file/) to be of interest._) – Jeff Zeitlin Feb 08 '21 at 18:55
  • See also: https://stackoverflow.com/a/55341293/45375 – mklement0 Feb 08 '21 at 22:08

0 Answers0