Darktux
Darktux

Reputation: 427

How to use the value of a variable as a variable itself?

i am trying to code in powershell where it needs i use a value of a variable as a variable itself ; is there a proper syntax to do it?

here is the example;

$color = "red"
$red = 1

how can i obtain the value of 1? $($color) $"$color" etc give out the errors.

I can explain more regarding the issue i am working with if needed.

Upvotes: 1

Views: 227

Answers (1)

Lance U. Matthews
Lance U. Matthews

Reputation: 16596

The syntax isn't very concise, but you can use the Get-Variable cmdlet:

Get-Variable -Name $color -ValueOnly

...or simply...

gv $color -ValueOnly

Upvotes: 4

Related Questions