Windows Vista Forums

Creating A Variable from the Value of Another
  1. #1


    Luke Guest

    Creating A Variable from the Value of Another

    I want to be able to create a variable from the value of another variable.
    For example, say that $a=example1. How could I create a variable named
    $example1 by using the data from $a?



      My System SpecsSystem Spec

  2. #2


    Kiron Guest

    Re: Creating A Variable from the Value of Another

    Use Set-Variable:

    Set-Variable -name $a -value 'new value'


    --
    Kiron

      My System SpecsSystem Spec

  3. #3


    Shay Levi Guest

    Re: Creating A Variable from the Value of Another

    PS C:\Scripts> $a="hello"
    PS C:\Scripts> New-Variable -name "$a" -value " world"
    PS C:\Scripts> $hi
    world

    Shay
    http://scriptolog.blogspot.com



    > I want to be able to create a variable from the value of another
    > variable. For example, say that $a=example1. How could I create a
    > variable named $example1 by using the data from $a?
    >




      My System SpecsSystem Spec

  4. #4


    Shay Levi Guest

    Re: Creating A Variable from the Value of Another

    sorry had a typo

    $a="hello"
    New-Variable -name "$a" -value " world"
    $hello
    world


    Shay
    http://scriptolog.blogspot.com



    > PS C:\Scripts> $a="hello"
    > PS C:\Scripts> New-Variable -name "$a" -value " world"
    > PS C:\Scripts> $hi
    > world
    > Shay
    > http://scriptolog.blogspot.com
    >> I want to be able to create a variable from the value of another
    >> variable. For example, say that $a=example1. How could I create a
    >> variable named $example1 by using the data from $a?
    >>




      My System SpecsSystem Spec

  5. #5


    A. Petitjean Guest

    RE: Creating A Variable from the Value of Another

    Hi Luke,

    You can also try this technique :
    $a="example1"
    $i=10
    Invoke-Expression "`$$a = $i"

    Best regards,

    Arnaud Petitjean
    http://www.powershell-scripting.com
    The french PowerShell Community

    "Luke" wrote:

    > I want to be able to create a variable from the value of another variable.
    > For example, say that $a=example1. How could I create a variable named
    > $example1 by using the data from $a?


      My System SpecsSystem Spec

Creating A Variable from the Value of Another problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating Environment variable during logon script Elgordo VB Script 1 15 Dec 2008
$Variable Is In {1,2,3,4} C S S PowerShell 3 23 Oct 2008
Creating objects with variable names in functions Frank PowerShell 4 14 Apr 2007
Set-Variable Fred J. PowerShell 3 25 Oct 2006
How can I ensure that a variable is a built-in powershell variable? Sung M Kim PowerShell 7 22 Sep 2006