replacing and adding values in a file

Welcome Forums General PowerShell Q&A replacing and adding values in a file

Viewing 3 reply threads
  • Author
    Posts
    • #258167
      Participant
      Topics: 2
      Replies: 2
      Points: 28
      Rank: Member

      I Have a file which may be empty or may contain one or two or all the three of  the following key value pairs:

      ===================

      var1 = “First”,

      Var2 = “second”,

      Var3 = 30

      ===================

      A part of my PowerShell script needs to take  inputs of one or two or three of the below three variables

      $Var1 = “Fifth”

      $Var2 = “sixth”

      $Var3 = 40

      The condition is , if the input variable is present in the file then just replace the value of it. For example if var1 is present in the file then replace the value of var1 with “Fifth” If it’s not present in the file then append a new line at the end.  While appending the new line a “,” needs to be added to the previous line. The file should not be overridden, only the value should be replaced or added as a new line.

       

    • #258248
      Participant
      Topics: 12
      Replies: 541
      Points: 1,314
      Helping Hand
      Rank: Community Hero

      Sample Code:

      Sample Output:

    • #258386
      Participant
      Topics: 2
      Replies: 2
      Points: 28
      Rank: Member

      Hi Sam,

      Thanks for the detailed script. I tried your script and it worked perfectly well. I had to add this sub part to my main script and was struck at it. Thanks a lot 🙂

    • #258389
      Participant
      Topics: 2
      Replies: 2
      Points: 28
      Rank: Member

      Hi Doug,

      Yes, I did. Sorry if my question was lacking clarity. It’s all good now.

      Thanks

Viewing 3 reply threads
  • You must be logged in to reply to this topic.