Replacing row in CSV file

Hello,

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…

 

$CREATE_CSV_FILE = @(

[pscustomobject]@{

"Computer Name" = $env:computername

"User Name" = $env:username

Date = Get-Date -Format "yyyy/MM/dd"

}

)

$CREATE_CSV_FILE | Export-Csv -Path C:\ProgramData\$env:computername.csv -NoTypeInformation

$CREATE_CSV_FILE | Export-Csv -Path C:\ProgramData\MainList.csv -NoTypeInformation -Append

 

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.

$newcsv = foreach($line in $csv)
{
    if($line."Computer Name" -eq $env:COMPUTERNAME)
    {
        [pscustomobject]@{
        "Computer Name" = $env:computername
        "User Name" = $env:username
        Date = Get-Date -Format "yyyy/MM/dd"
        }
    }
    else
    {
        $line
    }
}

$newcsv | Export-Csv $csvfile -NoTypeInformation

Another approach:

#Emulate a csv
$csv = @"
"Computer Name","User Name","Date"
"Computer1","User1","2020/10/21"
"DESKTOP-MGT9HIB","rasim","2020/10/19"
"Computer4","User3","2020/10/19"
"Computer3","User5","2020/10/17"
"@ | ConvertFrom-Csv

$newCsv = @()
#Filter out the computer
$newCsv += $csv |
           Where {$_."Computer Name" -ne $env:COMPUTERNAME}
#Add a new object with the computer
$newCsv += [pscustomobject]@{
    "Computer Name" = $env:computername
    "User Name" = $env:username
    Date = Get-Date -Format "yyyy/MM/dd"
}
 
$newCsv # | Export-Csv ...

Output:

PS C:\WINDOWS\system32> $csv

Computer Name   User Name Date      
-------------   --------- ----      
Computer1       User1     2020/10/21
DESKTOP-MGT9HIB rasim     2020/10/19
Computer4       User3     2020/10/19
Computer3       User5     2020/10/17

PS C:\WINDOWS\system32> $newCsv

Computer Name   User Name Date      
-------------   --------- ----      
Computer1       User1     2020/10/21
Computer4       User3     2020/10/19
Computer3       User5     2020/10/17
DESKTOP-MGT9HIB rasim     2020/10/24