Consider (probaply) better alternatves:
$Hash=@{Cart="text cart";Cat="text cat";Home="text home"}
$var = [PSCustomObject]$hash
$hash
$var
$names = "Cart","Cat","Home"
foreach ($name in $names){
"`$hash['{0}']={1}" -f $name,$hash[$name]
"`$var.{0}={1}" -f $name,$var.$name
}
Sample output:
Name Value
---- -----
Cart text cart
Cat text cat
Home text home
Cart Cat Home
---- --- ----
text cart text cat text home
$hash['Cart']=text cart
$var.Cart=text cart
$hash['Cat']=text cat
$var.Cat=text cat
$hash['Home']=text home
$var.Home=text home
With this approach(es) you also don't need to know the names beforehand,
you can iterate them with:
$hash.Keys
or
$var.PSObject.Properties.Name