Author Posts

July 13, 2017 at 9:51 pm

$Var1 = "abc"

$Var2 = "$Var1"

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

July 14, 2017 at 12:06 am

More context about what you're trying to accomplish would be very helpful 🙂

July 14, 2017 at 5:25 am

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)"

Results:

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