0

I've been struggling with this issue for a while and searched for hours but can't find it how to solve it

Problem: All I want is basically to get a string, and then use it as a variable name.

Example:

Eval('SomeText') = 'someinfo'

Which would out come like:

$SomeText (which contains 'someinfo')

Maybe a little bit hard to explain hope you guys understand it, it's probably something simple but I just don't know how to do this in PowerShell scripting.

7
  • $SomeText = 'someinfo' isn't working? I think I am misunderstanding this. Oh! Do you mean setting the name of the variable based on results of another operation? Commented Dec 2, 2014 at 9:53
  • yes exactly setting the name of the variable based on the result of another operation (sorry for the language barrier) Commented Dec 2, 2014 at 10:00
  • All good - so, you can use Set-Variable to do this. Example: Set-Variable -Name (Eval('SomeText')) -Value 'someinfo' Commented Dec 2, 2014 at 10:03
  • Thanks Jeeva Set-Variable -Name ´SomeText' -Value ´someinfo' is working flawless! appreciate your help! Commented Dec 2, 2014 at 10:09
  • Not a problem! Have a good day. Commented Dec 2, 2014 at 10:11

1 Answer 1

1

You can use Set-Variable to set a variable variable name, as shown below.

PS> Set-Variable -Name ($env:COMPUTERNAME) -Value "sometext"
PS> Get-Variable -Name ($env:COMPUTERNAME)

Name                           Value
----                           -----
jr-pc                          sometext

You can also refer to the variable with (in this case):

$($env:COMPUTERNAME)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.