Replacing row in CSV file

Welcome Forums General PowerShell Q&A Replacing row in CSV file

Viewing 2 reply threads
  • Author
    • #265229
      Topics: 8
      Replies: 4
      Points: 96
      Rank: Member


      The script below creates a CSV file named after the computer name and then creates a row with the computer name, user name and date.   Each time the script is ran, that file will simply be replaced which is the desired behavior.

      However, the script will also create another file where it will also add that information, however, I want that information in that row to be replaced.   The reason being is when I run the script on multiple machines, I want them to create their own unique row in this file and every time the script is ran, it would just replace that row for the specific computer name and not just keep appending to the file with the same computer name information or replacing the file outright.

      So in short, I am wanting to do a search and replace on the row of the main file where if “$env:computername” exists then replace that line with the updated information for the user name and date as an example.

      Sorry for the long and probably horrible explanation, but any help would be greatly appreciated as I haven’t had any luck in find this specific example when it comes to manipulating a CSV file.

      Code below…



    • #265631
      Topics: 9
      Replies: 674
      Points: 2,665
      Helping Hand
      Rank: Community Hero

      It seems you’ll have to read all the lines, replace the line that matches, then write the file again. I would be concerned about conflict if two or more machines try to access the file at the same time. You might consider just having a central machine that handles reading from each computers local file and updating the shared shared file. Something like this should work if there is no conflict.

    • #265730
      Topics: 16
      Replies: 1842
      Points: 3,514
      Helping Hand
      Rank: Community Hero

      Another approach:


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