You can do it at least three ways. Maybe more.

With invoke-expression:

                $foo = "bar"
                Invoke-Expression "`$$foo = 22"

with Set-Variable:

                Set-Variable $foo -Value 22

Using New-Item is left as an exercise for the reader.

Of the choices, I would suggest that Set-Variable is easiest to read and 
comprehend. :)

From: Joseph L. Casale [mailto:[email protected]]
Sent: Tuesday, May 29, 2012 10:40 AM
To: NT System Admin Issues
Subject: Powershell Variables

How does one leverage the evaluated output from some expression as the name of 
a new variable?

For example (simplified):

$foo = "bar"
$bar = 22

Write-host $"$foo"
 22

Thanks!
jlc

~ Finally, powerful endpoint security that ISN'T a resource hog! ~
~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/>  ~

---
To manage subscriptions click here: 
http://lyris.sunbelt-software.com/read/my_forums/
or send an email to 
[email protected]<mailto:[email protected]>
with the body: unsubscribe ntsysadmin

~ Finally, powerful endpoint security that ISN'T a resource hog! ~
~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/>  ~

---
To manage subscriptions click here: 
http://lyris.sunbelt-software.com/read/my_forums/
or send an email to [email protected]
with the body: unsubscribe ntsysadmin

Reply via email to