How can I assign a value to a variable whose name is in the form of a str val?

$Var1 = “abc”

$Var2 = “$Var1”

Without referencing $Var1 directly and only using $Var2’s string content, how do I assign “def” to $Var1?

More context about what you’re trying to accomplish would be very helpful :slight_smile:

You would set $var2 as a reference to $var1

$var1 = "abc"
$var2 = [ref]$var1

Write-Host -ForegroundColor Cyan "Initial Values"
"`$var1 = $var1"
"`$var2.value = $($var2.Value)"

Write-Host -ForegroundColor Cyan "Set New Value via `$var2.value"
$var2.Value = "def"

"`$var1 = $var1"
"`$var2.value = $($var2.Value)"


Initial Values
$var1 = abc
$var2.value = abc
Set New Value via $var2.value
$var1 = def
$var2.value = def