Hello there! Sorry to hear that you are having difficulties replacing an existing value. I ran a quick test and was able to use pretty much the same code. I chose a single variable from the list and was able to update the value. The only change was the for loop, all function calls were unchanged.
# Get reference to project
$octopusProject = Get-OctopusProject -OctopusServerUrl "https://shawnsesna.octopusdemos.app" -ApiKey "API-XXXXXXXXXXXXXXXXXXXXXX" -ProjectName "SSIS Example"
# Get list of existing variables
$octopusProjectVariables = Get-OctopusProjectVariables -OctopusDeployProject $octopusProject -OctopusServerUrl "https://shawnsesna.octopusdemos.app" -ApiKey "API-XXXXXXXXXXXXXXXXXXXXX"
foreach ($octoVariable in $octopusProjectVariables.Variables)
if ($octoVariable.Name -eq "CM.sql2016.AdventureWorks2017.sa.UserName")
$octoVariable.Value = "shawn.sesna"
# Update the project
Update-ProjectVariables -ProjectVariables $octopusProjectVariables -ApiKey "API-XXXXXXXXXXXXXXXXXXXXXXX" -OctopusServerUrl "https://shawnsesna.octopusdemos.app" -VariablesId $octopusProjectVariables.Id