Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

11 Nov 2010

Get variable value from variable with PowerShell

Sometimes you only know at runtime in which variable a certain value is stored. Let me clarify with an example:

$tim = 30;
$evy = 24;
$name = Read-Host "Enter your name";

In essence, if $name equals tim we want to use $tim and if $name equals evy we want to use $evy. This can be achieved with Get-Variable:

$age = Get-Variable $name -valueOnly;
Write-Host "Your age is $age";