Two variables into on Variable

I have a question that I hope can be answered. I need to create a dynamic variable as such
$m = “this”
$n = “that”

$m+$n = “toes in the sand”

Write-host $m$n = “toes in the sand”

So basically I need $m and $n to create $thisthat as a variable name.

Thanks for any help!

I have a question that I hope can be answered. I need to create a dynamic variable as such

$m = “this”
$n = “that”

$m+$n = “toes in the sand”

Write-host $m$n = “toes in the sand”

So basically I need $m and $n to create $thisthat as a variable name.

Thanks for any help!

Something like this?

PS C:> $one = ‘This’
PS C:> $two = ‘That’
PS C:> New-Variable -Name “${one}${two}” -Value ‘Toes in the sand’ -Force
PS C:> $ThisThat
Toes in the sand

Something like that… but I won’t know that $thisthat is the variable to type it?

Does that make sense?

In the script I wont be able to write $thisthat cause it could potentially be

$appleorange
$himher

all depends on the values put in.

Ok, so continuing the above, but replacing the last line:

PS C:> $v = New-Variable -Name “${one}${two}” -Value ‘Toes in the sand’ -Force -PassThru
PS C:> $v.Value
Toes in the sand
PS C:> $v.Value = ‘something else’
PS C:> $v.Value
something else

Depending on your needs, hashtables may also be a viable way to do this, where you build the keys using user input and then reference the values using the combination of those keys, like this:

PS C:> $ht = @{}
PS C:> $ht[“${one}${two}”] = ‘Toes in the sand’
PS C:> $ht.“${one}${two}”
Toes in the sand
PS C:> $ht.“${one}${two}” = ‘Something else’
PS C:> $ht.“${one}${two}”
Something else

maybe I am not explaining just right… it is like this…

function createbox ($boxname)
{
$objText$boxname = New-Object System.Windows.Forms.TextBox
$objText$boxname.Location = New-Object System.Drawing.Size(130,20)
$objText$boxname.Size = New-Object System.Drawing.Size(110,20)
$objForm.Controls.Add($objText$boxname )
$objText$boxname.Text = get-content env:computername
}

createbox (NetBios)
createbox (Display)

Now $objText$boxname does not work.

But I need that to be a variable that really is $objTextNetBios or $objTextDisplay

does this make more sense?

I

What part of what I already provided doesn’t give you that? Using New-Variable, I am actually creating a variable with a dynamically generated name. Then I show you how you can set that variable as well. But you can’t easily do dynamic variable name generation like you are trying to do because, as you indicated, it does not work (PowerShell does not support this).

Once you’ve created your dynamically named variable, you can do whatever you want to with that object, including access its properties, change their values, etc.

In the example I created, there actually is a variable called ‘ThisThat’, and you could reference that variable by name using $ThisThat, but as you said, you can’t be dependent on the name, so the *-Variable cmdlets allow you to work with dynamically named variables in that case.

You could also use Invoke-Expression, but I stay away from Invoke-Expression if possible as a best practice.

I would be interested in seeing an example of this.

i am trying to find a way to call those dynamically created variables again so i can set additional attributes. Basically it is a number of servers (fixed array) that i want to return the sites hosted on them, (checkboxes for each site) i would like to make this part dynamic since the sites can come and go.

I am using this command:

New-Variable -name $site -value (New-Object System.Windows.Forms.CheckBox)

but i have no idea how to call that $site again to control placement, label, etc. of the checkbox

Once you create a variable like that, you can get it using Get-Variable. Or better, when you call New-Variable, use -PassThru to return the variable so that you catch it.

That is to say, do this:

New-Variable -name $site -value (New-Object System.Windows.Forms.CheckBox) $checkbox = Get-Variable -Name $site -valueOnly $checkbox.Enabled = $false

or this:

$checkbox = New-Variable -Name $site -value (New-Object System.Windows.Forms.CheckBox) -PassThru $checkbox.Value.Enabled = $false

In the latter case, $checkbox is the variable (not the variable’s value), so you need to use the Value member to access the actual Checkbox object. In the former case, you’re getting the value directly by using -ValueOnly in the call to Get-Variable, so you can avoid the .Value member.

Does that help?

hhmmm… i am going to give that a whirl. the issue i was running into is on the first run it works, but on the next run it over wrote the last one. but in looking at the first sample you have that may may allow me to better position the checkboxes and place specific controls on each one. i will work it a bit and let you know what i come up with.

Thanks.