Dynamic variable and value assignment in powershell -


how can declare variables , assign values them @ run time.

reason: fetching these variables values sql server , these variable values configurable in nature

code have tried till now

   [array]$vararray = @($($servername),$($hostname))    foreach($varname in $vararray)         {           $varname = "some test value"         }  write-host $servername write-host $hostname 

the simplest way of using dynamically named variables dictionary:

$vars = @{}  # create empty dictionary  # add key/value pairs dictionary: $vars["foo"] = 23 $vars["bar"] = "foobar" $vars["baz"] = get-content c:\sample.txt 

another way declare variables on fly:

$name  = "foo" $value = "bar"  new-variable $name $value  echo $foo 

or create custom object , add properties kyle c suggested. approach similar dictionary, although technically different.


Comments