Although your example looks like JSON, it is not, so what you can do here is to read the file as single multiline string and use regex operator -replace on its content.
The easies way to retain the replacement's (multiline) format is by using a Here-String:
# replace with
$newCars = @'
Car {
Audi,
Mercedes,
}
'@
# find the empty Cars node and replace it with above $newCars
# if you also want the output on screen, append switch `PassThru` to the Set-Content command
(Get-Content -Path "local.tfvars" -Raw) -replace '(?m)^Car\s*\{\s*}', $newCars | Set-Content -Path "local.tfvars"
Result:
Car {
Audi,
Mercedes,
}
Bus {
}
Regex details:
(?m) Match the remainder of the regex with the options: ^ and $ match at line breaks (m)
^ Assert position at the beginning of a line (at beginning of the string or after a line break character)
Car Match the characters “Car” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\{ Match the character “{” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
} Match the character “}” literally
.tfvarsareJsonfiles) theConvertFrom/ConvertTo-Jsoncmdlets.