I’m trying to make PowerShell interact with a CSV file and update it’s values. There are four columns in the CSV: “Object Name”, “Times Flagged”, “Date Last Flagged”, and “Notes” (the last one intended to be manual entry/editing only). Currently, the script gets a list of object names from a txt file, and compares them to all the values under “Object Name”, and is able to figure out which objects already exist on the CSV and which do not.
What I’m currently trying to get it to do is update the existing entries by increasing the “Times Flagged” value by 1, and changing the “Date Last Flagged” to the current date. I decided to work on getting it to add “Date Last Flagged” first before figuring out “Times Flagged”.
Here’s my code as of right now:
$listFile = Get-Content "\File PathTest List.txt"
$csvFile = Import-Csv -Path '\File PathTest Report.csv'
$Now = [datetime]::Now
foreach($object in $listFile)
{
if($csvFile | Where-Object {$_.'Object Name' -eq $object}) #Updates existing entries
{
Where({$_.'Object Name' -eq $object}).'Date Last Flagged' = $Now
}
else #Adds new entries
{
}
}
When running the script as it currently is, the host spits out error messages for each existing entry it attempts to update.